are_available(builtin)

Some time ago, I decided to stop making features for the Copper interpreter engine, but I ran across a situation in coding where I came up with an idea for a very convenient built in function.

Sometimes you want to know if a function is available to be used. On the Copper side, you can use “is_member” to check if a member belongs to a function-object without disturbing the state of the function-object. Since Copper function-objects are all tied to variables instead of instantiated, we don’t need functions like Python’s “inspect” (which checks modules).

But what if you want to check the availability of built-in functions and foreign functions? You might assume some built-in functions are always there, but technically, some of them (particularly math functions) may have different names depending on compile settings of the engine. And the inclusion of foreign functions (which are, well…, central to making Copper useful for anything) is “invisible” to the person programming on the Copper side.

We could check to see if the name refers to a variable with an empty function, but that doesn’t help us because an empty function is returned for foreign functions called directly. For example:

print(are_empty(byte)) # Byte is an extension function that can be added)

… will print empty function. Assuming the foreign function has been added to the engine, we will get a printed warning, but this warning is undetectable within the Copper code.

Another approach would be to check for empty function being returned. But this calls the function itself, which we don’t want to do. First, if the function is a foreign function, then it will try to perform, perhaps sooner than we want (because we only want to check its presence at this point). Second, if it is a foreign function and we don’t provide it with the right arguments, the engine will halt from an error.

The only real solution to this problem is for there to be a built-in function that can tell you if a built-in function or a foreign function are available. No guessing in the dark, no waiting to see if the function returns what you want. This new function is called “are_available”. Why not “is_available”? In the spirit of LISP that Copper follows, it’s nice when functions can accept an “unlimited” number of arguments.

if ( are_available("foreign1", “foreign2”, “foreign3”) ) { foreign1( foreign2:, foreign3: ) }

This way, we can check as many functions as we need before trying to use them. This might be helpful if you want to use an XML parser and a file opener and need to see if the foreign functions for them are even available.

This function was easy to implement and is now part of the Copper engine version 0.642, but to get it you will need to wait for the next publication to Github (or wherever I end up hosting it next).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s