Few things II
Jari-Matti Mäkelä
jmjmak at utu.fi.invalid
Wed Aug 8 05:55:07 PDT 2007
Jari-Matti Mäkelä wrote:
> bearophile wrote:
>
>> 12) I have seen D allows to pass anonymous functions like:
>> (int x){return x*x;}
>>
>> C# V.3.0 uses a more synthetic syntax:
>> x => x*x
>>
>> That syntax isn't bad, but I don't know how the compiler automatically
>> manages the types in such situations, it can be a bit complex.
>
> It does a bit more type inference than D at the moment. Currently you
> either get return type or parameter type inference, but not both:
>
> (int x){return x*x;}
>
> int mul(T)(T x) { return x*x; }
I lost my train of thought a bit here. I'm not sure if this works, but
(T)(T x) { return x; }
could allow type inference in both ways. Still on the parameter side the
polymorphism is explicit and you can't "chain" the literals because there's
no implicit parametric type in D afaik:
auto myfun = (x) { return "foo"; }
auto myval = myfun(myfun(42));
A bad example, but this kind of code would also benefit a lot
template foo(T : int) { alias char[] foo; }
template foo(T : char[]) { alias int foo; }
template zzz(T : int) { const zzz = "hello"; }
template zzz(T : char[]) { const zzz = 0; }
U bar(T, U = foo!(T))(T t) {
return zzz!(T);
}
/* with proper type inference
def bar(t) {
static if (is(t : int)
return "hello";
else
return 0;
}
*/
void main() {
auto a = bar(42);
auto b = bar("hi");
pragma(msg, typeof(a).stringof); // char[]
pragma(msg, typeof(b).stringof); // int
}
I've even seen wicked code that in the end looked pretty much like this
U bar(T, U = inferType!(parseAST!(scanSource!(traits!(getSourceOfThisFun
())))) )(T t) {
...
}
What can I say - somebody must have had fun :P
More information about the Digitalmars-d
mailing list