Implicit Arguments

Normally the way to pass an "implicit argument" to a function is by having the function refer to a global variable (making it as if you had passed the global variable directly in an argument). However, this technique breaks referential transparency. And in order to pass a different value to the function, you must first set the global variable, then call the function, then change it back. This technique, in addition to being verbose, is effectively dynamically scoped which is considered undesirable.

I propose a way to pass arguments implicitly in a statically-scoped manner.
Scan the types of the arguments passed to a function, left to right. If the type of an argument does not match the expected type of the argument (as defined in the function definition), search the scope of the function call for the nearest variable of the expected type. Insert that variable as an argument to the function and continue scanning (starting again from the argument whose type did not originally match).

What I mean by nearest is that if there are 2 variables of the appropriate type in scope, choose the one that was put into scope farther down in the code.

Example:
Suppose I have a function to calculate the speed of a "bird" object, given its x and y coordinates (I'm using pseudocode here)
function birdspeed(bird b){
return sqrt(square(vx(b))+square(vy(b)))
}
This could become, instead,
function birdspeed(bird b){
return sqrt(square(vx)+square(vy))
}
where the argument b in the functions vx and vy has been made implicit, since b is the nearest variable of type "bird."

This example is pretty trivial, saving only a few keystrokes at a possible expense of clarity. However, in some situations it could be pretty convenient. I thought of this idea based on SAS's use of datasets; you initialize a dataset and then you can perform operations on it without referring to it explicitly. For instance you might say "plot y = x" and not "plot y = x using mydataset." The purpose of this approach is to establish an evaluation context where certain frequently referred to things are considered implicit and unnecessary to restate (such as, in SAS, the dataset you're using).

© Bart Parkis
Last modified:Tue May 19 19:07:52 2009