Saturday, July 10, 2010

Regarding function ambiguity

Yet another ambiguity situation can be seen in PineDL. That being the function type vs. function declaration.

Consider the following (valid) expression:
function() return 0;
The above being a function that takes no arguments and returns 0.

Now, consider this in context:
var foo = function() return 0;;
Note that the double ';' is needed(the first to end the function statement, the other to end the variable declaration).

The 'function' keyword is, however, a type as well.
const mytype = function;
Being a type constant, operations can be applied to function, including calling.
const x = function();

Now, consider this piece of code:
x = function();;
What can this be?
1. Function constant calling, assigning the result to variable x, and finally an empty statement.
2. Function declaration, assigning the function to x.

But which?
Of course, some cases are obviously one of the two:
{var x = function();} //Obviously 1
var x = function(){}; //Obviously 2

But the problem is in edge cases.
There are a few solutions:
1. Demand functions to be declared with {}
2. Prefer one of the cases(problematic)
3. Demand functions to be declared with {}, but allow functions to be declared using the \foo() syntax, and allow those to have no {}. In addition, make sure \ is not a valid type constant.

I haven't decided which yet.

No comments:

Post a Comment