Higher Order Functions
David Hakim <dhakim-Gkm/TONP9n1Wk0Htik3J/[email protected]> Wed, 12 Feb 2003 10:53:47 -0500
| Newsgroups | gmane.comp.lang.moto.devel |
|---|---|
| Message-ID | <[email protected]> |
Well, I really just can't help myself. I've evaluated the code changes
needed for a first phase implementation of HOFs (only those as powerful
as C function pointers) and its just too easy for me not to do :) Here
is a brief outline of the syntax changes I plan on supporting in the
first phase. I am only going to implement HOF declarations, casts and
values in the first phase. Future phases may include anonymous function
definitions and partially completed of function calls.
Declarations of 'function' typed variables will look like those that
follow. The function variable name will follow the type. Since
functional types are indistinguishable at parse time from function
calls we will need to determine expression validity in motov.
int () func
int (String) func
int (String,char) func
int () () func
int (String) () func
int () (String) func
int (String) (char) func
int (String,char) () func
int (String,char) (float) func
int (String () ) func
int (String (char) ) func
Just like all other reference types we will be able to cast functional
types to and from objects
<int () >
<int (String) >
<int (String,char) >
<int () () >
<int (String) () >
<int () (String) >
<int (String) (char) >
<int (String,char) () >
<int (String,char) (float) >
<int (String () ) >
<int (String (char) ) >
Like C we will use the & operator to specify that we are interested in
a function's address, not in its evaluation. The necessity for this
extra token is clear since there isn't another good way to
differentiate a no-arg function address from a call to that function.
The other important thing to notice here is that since unlike C,
functions in moto are uniquely identified by the function name AND
arguments, those argument types must be specified when referencing the
function definition.
&f ()
&f (<String> ?)
&f (<String> ?, <int> ?)
&f (<int () > ?)
&f (<int (String) > ?)
&f (<int (String,char) > ?)
&f (<int () () > ?)
&f (<int (String) () > ?)
Anonymous Function Calls
{ f(&1,&2); }
{ f(&1,&2); return &1; }
Partially evaluated HOFs
&f ("funk")
&f (<String> ?, 27)
&f ( &g (<Object> ?))