Sunday, September 07, 2008

Mean and lean

Wouldn't be a good thing if you were allowed to define functions like these in C#?
public long fact(int n) =>
n <= 1 ? 1 : n * fact(n - 1);
operator+(Complex c1, Complex c2) =>
new Complex(c1.Re + c2.Re, c1.Im + c2.Im);
Actually, you are allowed even riskier tricks when dealing with inline lambdas. The uniformity principle states that, at least, some of these tricks should be accepted while defining regular functions. And that's what Freya does.
By the way, there are no omissions in the operator definition... by Freya standards, of course. Operators are always public and static, so why bother the user with stating this each and every time. On the other hand, the return type has been omitted: it can be statically deduced from the defining expression. Freya has precise rules for these static inferences: you can omit the return type when the expression is a constructor call, a typecast or, recursively, when it's a common expression block wrapping an expression with static inferable return type or a conditional with identical inferred types in both of its branches.

Labels: , ,


Post a Comment

<< Home