To & or not to &
David Hakim <dhakim-Gkm/TONP9n1Wk0Htik3J/[email protected]> Mon, 3 Mar 2003 18:22:51 -0500
| Newsgroups | gmane.comp.lang.moto.devel |
|---|---|
| Message-ID | <[email protected]> |
This is my question ... I'm trying to consider whether I can get rid
of the & used for function identifiers. Right now to get the 'address
of a function' in moto you need to precede the function name with an &
e.g.
int(String) myatoi = &atoi(<String> ?)
The above case would be clear enough without the &
int(String) myatoi = atoi(<String> ?)
and in fact since there is only one variant of the atoi function
neither of the following would be ambiguous either
int(String) myatoi = atoi(?)
int(String) myatoi = atoi
The problem with doing away with the ampersand comes up with basic
function pointers when we have a function with two forms, one that
takes no arguments, and one that takes one or more arguments
void func(){ print 3; }
void func(int i){ print i; }
Object myfunc;
myfunc = func;
'func' in the last case has the potential at least to be ambiguous. We
could make a rule that 'when multiple variants of a function exist, the
function name alone identifies the zero-argument variant'
However while the previous rule may disambiguate function pointers, I
don't think it does so in a very intuitive way ... Also, it doesn't
begin to address the issues we run into with partial function
application.
Partial function application is like super currying (its probably
really just a type of closure ... I don't know enough about functional
terminology to say this for sure though :) ) . The idea here is to
create new functions by 'fixing the inputs' of more general functions.
Consider a function
double pow(double,double)
which returns the first input to the power of the second input. With
partial application I could define the following two functions
double(double) square = pow(?,2);
double(double) twoToThePow = pow(2,?);
Thats pretty darn powerful stuff :) . Now suppose we have a one arg
function with side effects:
global int count;
void hello(String s ){ print "hello "+s +" "+count++; }
Its clear that calling the above function has the side effect of
incrementing count. Suppose I want to use the mechanism of partial
application to 'fix' the function's one input creating another function
I can call later.
void() myhello = hello("Dave");
but that won't work because the type of the expression 'hello("Dave")'
is 'void' and not 'void()' i.e. the return value of the hello function.
I need the '&' to distinguish between a function I'm calling right away
vs a function I'm defining to call later
void() myhello = &hello("Dave");
Or do I ? With any one of the anonymous function syntaxes I've
discussed with people I could accomplish the same thing by doing:
void() myhello = <void()>{hello("Dave");} ;
or maybe even
void() myhello = {hello("Dave");} ;
That is, if I can convince the parser not to expect an inline array
declaration when it sees '{' :) . So its at least clear to me that I
can do away with the necessity for '&' once I have a way to create more
general anonymous functions. At the same time '&' may be clearer and
shorter in at least some circumstances.
What do you guys think ? Should the '&' stay or go ?
-Dave