www.digitalmars.com         C & C++   DMDScript  

D - casting delegates

reply "Daniel Yokomiso" <daniel_yokomiso yahoo.com.br> writes:
Hi,

    Since assigning inner functions to delegates is valid, shouldn't casting
them to delegates be valid too?


void foo(int function() lambda) {
    printf("foo(int function() lambda) is %d\r\n", lambda());
}
void foo(int delegate() lambda) {
    printf("foo(int delegate() lambda) is %d\r\n", lambda());
}
void main() {
    int function() _function = function int() {return 42;};
    int _closure() {return -42;}
    int delegate() _delegate;

    _delegate = _closure;
//    _delegate = _function;

    foo(_function);
    foo(_delegate);
    foo(cast(int function()) _closure);

//    foo(_closure);
//    foo(cast(int delegate()) _closure);
//    foo(cast(int function()) _delegate);
//    foo(cast(int delegate()) _function);
}


    Commented lines give us errors. I fail to see why they shouldn't be
considered correct. Since we can assign _closure to _delegate, and cast
_closure to "int function()" we should be able to assign _function (or at
least cast) to _delegate. Also using this same "intuitive" rule we could
call "foo(_closure)" directly resulting in a "void foo(int delegate()
lambda)" call. Using this same "obvious" rule we can say that casting
_delegate to _function should be possible. Hmmm perhaps these two types
(function and delegate) should be equivalent.
    BTW what is the semantical difference (not implementation difference)
between "function" and "delegate" (as types not as keywords)? In functional
languages there's a semantic difference between closures* (functions +
environment) and combinators** (functions without environment), but both
have same type definitions (i.e. always something like T -> U, regardless of
environmental baggage). A type unification should be a Good Thing, perhaps
we could even eliminate inner functions, or define them to be sugar for
"const function" (Should this be supported? IMO function literals are
constant expressions) as _closure can be casted to _function.

* http://wombat.doc.ic.ac.uk/foldoc/foldoc.cgi?combinator
** http://wombat.doc.ic.ac.uk/foldoc/foldoc.cgi?closure

    Best regards,
    Daniel Yokomiso.

"Programming in Basic causes brain damage."
 - Edsger W. Dijkstra



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.463 / Virus Database: 262 - Release Date: 17/3/2003
Mar 27 2003
parent "Walter" <walter digitalmars.com> writes:
In the current state of D, you cannot convert between functions and
delegates or vice versa. That comes about because of the way they are
implemented. I know of a way to unify the two, but it is a ways away from
being implemented.
Mar 30 2003