Re: How would you add HOFs to moto ?

"Charles Patterson" <cpatterson-N2D8602I6TJWk0Htik3J/[email protected]> Mon, 14 Apr 2003 15:58:51 -0400
Newsgroups gmane.comp.lang.moto.devel
Message-ID <01a301c302c0$48578ee0$8b14a8c0@cpatterson>
Heh.  Finally getting around to a thorough reading.  Impressive stuff!  I
know some of this has been coded and released, but I don't think "anonymous
functions" have so I thought I'd throw in a word.

> 8) Anonymous Functions :
>
> <double(double)> { return @1 * @1; }; // squares then returns the one
> input
> <int(String)> { return @1 eq @2 ? 0 : @1 gt @2 ? 1 : 0 ; }; // strcmp
>
> I'm introducing the concept of context sensitive variables here which
> I'm not entirely comfortable with.

Yeah, that would be less elegant than Moto IMO.  One alternative would be to
allow the
function definition form without a name:

    double (double x) { return x * x };

Since no function name is given it is implied anonymous.  While I can
appreciate your syntax that "casts" the gunk in curly braces to a function,
you lose your formal variable names as you point out.  And I see this as a
pseudo-cast, really, so it might be confusing.  However, by making it look
more like any other function declaration, I think I could remember it
easily.

While I'm not a big fan of the question mark in Moto, here is similar
alternative:

    double ?(double x) {return x * x };

> Bonus Problem : Can / Should the '&' token be removed from the above
> syntax ?

After a thorough reading, I am 50/50 on the & token.  I like it fine because
I'm a C++ programmer, but it does stand out as "low level" in Moto to me.

This would require some loss of backward compatability, but while you are on
the subject of all this function syntax, perhaps you could switch a regular
function declaration to:

    double(double x) f = { return x * x };

Get it?  You've already devised a type def for a function -- double(double
x).  This new syntax for declaring a function looks like
any other type def now:

    int i = 4;
    String s = "hello";
    double(double x) f = { return x * x };

Elegant!

    // a straight function (pointer) like any other object (pointer)
    double(double x) r;

    // a straight anonymous function as a param
    compute( double(double x) { return x * x } );

    // a function (pointer) assignment
    r = f;

    compute( r );

Overloading should work as it already does to pick the appropriate function.

> 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;

Yeah, I still think that languages where everything can be an "Object" will
be the death of us all.  (-:  It looks like you need the & operator just for
this one case.  Stepping back, you have function overloading which is
elegant and an Object that can point to anything which is elegant.  Having
both will muddy up your elegance.  I hate irony.  That is, you don't have to
specify which version of 'func' you mean when assigning it to another
function, so you never need the long form -- func(int i).  But due to the
Object concept, you have no left-hand information.  Two ideas:

1) Assuming the case above will be rare, I guess you could write Moto around
an optional & for all other cases, but require it for this one, ambiguous
case.  Lots of work, plus the & snuck in, even if rarely used.

2) require casting in this one case:

    Object f;
    f = <void(int i)> func;

Later,
Charlie