Re: To & or not to &
"Charles Patterson" <cpatterson-N2D8602I6TJWk0Htik3J/[email protected]> Wed, 5 Mar 2003 11:37:53 -0500
| Newsgroups | gmane.comp.lang.moto.devel |
|---|---|
| Message-ID | <001301c2e335$949b6920$8b14a8c0@cpatterson> |
> >> 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");} ;
> >
> > Looks good (except I wish it didn't have the curly braces)
>
> Without curly braces I may be able to turn a single expression into a
> function. But how would I be able to define a function inline that was
> made up of a whole series of expressions / statements one after the
> other ?
Converting a single 'expression' to a function sounds right to me. I guess
you are saying that, in the above example, you don't directly assign a
function to another, but you assign an entire code block to another. In the
above case, it happens that the entire block contains one function. I don't
think that will work. I think the code block will resolve to the return
from the function, which is "void", then you will try to cast that to a
function.
So...
> > , but will you
> > ever need this new syntax when calling the function immediately? For
> > instance,
> >
> > Object foo(String s){ return an object that is really a function
> > ptr; }
> >
> > void() bar= <void()>{foo("Dave");} ;
> >
> > hello would return an Object but you know it is really a function
> > pointer
> > and want to convert it.
>
> The above example confuses me :)
Perhaps I wrote it wrong. I think there is ambiguity as to what you are
assigning to bar. Is it the partially completed foo("Dave"); or is it the
return from foo("Dave") casted to a void().
In Moto, Object is the base class of everything, correct? This way, you can
have an array of mixed type, call it "Object[] A". Eventually you will cast
an element back to a useful type as in "int e = <int> Object[3]". Likewise,
if a function returns an Object, it may be a function ptr and need casting.
So I don't know if you are assinging foo("Dave") to bar, or if you are
casting the return Object of foo("Dave") to a void() and assinging it to
bar.
One more thought. Compare this problem to arrays and initialization. There
is a different syntax to know wether you are initializing it or calling it:
int[] A = { 3, 5, 6 };
int i = A[0];
So, likewise, perhaps you could have two syntaxes for something as important
as functions:
Assuming <> were unused in Moto:
// <> brackets would mean you are defining or referring to functions
String F1<String S> { return "output: " + S; } // define F1
String<String> makefunc<void> { return F1; } // define makefunc to
return a function "pointer"
// but () brackets would mean you are calling functions
String r = F1("Dave"); // call F1
// the culmination
String F2<void> = F1<"Dave">; // just (partially) assinging F1 to F2
String r = F1("Dave"); // calling F1
String F3<String> = makefunc("Charlie"); // calling makefunc and
assinging return to F3
String<String> F4<void> = makefunc<void>; // just assigning makefunc
to F4
sort( A, compare<int> ); // calling sort with function "pointer"
compare