Primitives in Generics
int19h at gmail.com
Fri Jul 9 15:36:26 PDT 2010
On Fri, Jul 9, 2010 at 3:23 PM, Neal Gafter <neal at gafter.com> wrote:
>> By the way, why do you say that the proposed lambda expressions aren't
>> function-valued? Is that because they don't have an expressible type?
> It is because they aren't of a function type, which is because function
> types have been dropped.
>> If so, then do you imply that C# lambdas aren't closures, either?
> C# lambdas are indeed of "delegate" types, which are function types.
C# lambda expressions and anonymous delegate expressions are not of
delegate types. C# 4.0 language specification, section 7.15 "Anonymous
"An anonymous function is an expression that represents an
“in-line” method definition. An anonymous function does not have a
value or type in and of itself, but is convertible to a compatible
delegate or expression tree type."
Consequently, you cannot e.g. write:
object f = (() => 123);
and VC# compiler will even state the reason rather bluntly:
error CS1660: Cannot convert lambda expression to type 'object'
because it is not a delegate type
Indeed, how could it be any other way, given that C# delegate types
are nominal (and not synthesized), and there could be many (or none!)
delegate types declared with a signature matching the lambda?
So far as I can see, C# has precisely the same limitations here as the
proposed no-function-types-but-SAM-conversion for Java. If this isn't
good enough to be properly called "closures", fine - though then the
definition of "closure" used is clearly different (and quite possibly
more correct, I don't want to argue this point) from the popular one.
The point, anyway, is that C# has had this construct, whatever the
name, for 5 years now, and it has a success story to tell. If Java
"closure-like construct" is on par with that, it's good enough for me
and all the people who have been happy with that aspect of C#.
More information about the lambda-dev