Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm creating a prototype scripting language similar to something like Javascript. In my language, every single reference to a variable is actually a function call (a getter function), whether it has parentheses or not. So for example, if do this:

var foo = 3;
var bar = foo;

What really happens is foo.set() is called on the first line, which creates a new getter function. In the second line, the newly created getter function for foo is called, which looks like this:

foo.get()
{
    return 3;
}

I can also assign a function to a variable, in which case that function BECOMES the getter function.

var foo = 
{
    return 4+5;
}
var bar = foo; // bar is now equal to the number 9, NOT the function that foo contains

So what I'm working on now is what an array of functions should return. For example:

function func1
{
    return 1;
}
function func2
{
    return 2;
}

var foo = new array();
foo.push(func1);
foo.push(func2);

var bar = foo; //what does bar contain? [func1, func2] or [1, 2]?

A reference to a single function runs that function and gives the return of the function. So I'm trying to decide if a reference to an array of functions should also run those functions, or if it would be more advantageous to just return the array of functions.

In other words, would the getter function for foo look like this:

foo.get()
{
    return [func1.reference, func2.reference]; //would return [func1, func2]
}

Or like this:

foo.get()
{
    return [func1(), func2()]; //would return [1, 2]
}

Specifically, I'm asking if anyone knows if there are problems or solutions that one method or the other would create? Are there any established rules of good programming language design that apply here?

share|improve this question

closed as primarily opinion-based by amon, MichaelT, gnat, GlenH7, Dan Pichelman Feb 24 at 15:10

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

2  
It doesn't matter if you don't have side effects (or only properly encapsulated side effects). However, most languages use eager evaluation, which would result in [1, 2] in your example. Good language design tip: Follow the principle of least surprise –  amon Feb 21 at 23:37
    
@amon it does matter, both from a type perspective and in a side-effect free context. From a type perspective because a function value is not equivalent to its application value (consequently, an array of functions is not an array of the values resulted from the application of each of its functions). And in a side-effect free environment, the second proposed option may yield different "results" when accessing the array, depending on the evaluation strategy (say, eager evaluation is adopted and one of the functions in the array does not halt vs. lazy evaluation, ditto). –  Thiago Silva Feb 22 at 2:13

1 Answer 1

up vote 2 down vote accepted

Use the first approach. Unless your language is pure functional, the second approach can have weird results. For example:

var x = 0;
function func
{
    return x;
}
var arr = new array();
arr.push(func);
var arr2 = arr;
x = 1;
print(arr[0]()); // prints "1"
print(arr2[0]()); // prints "0"

Or:

function func
{
    print("hello");
}
var arr = new array();
arr.push(func);
var arr2 = arr; // prints "hello"

PS: Are you sure you want to construct a new array every time you read an array variable?

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.