2 ms·
Expression bodied properties: the syntax feels slightly off for me because I can't really read the => the way I do in expression lambdas, as 'goes to' - it lack
by jameshart 13y ago
Expression bodied properties: the syntax feels slightly off for me because I can't really read the => the way I do in expression lambdas, as 'goes to' - it lacks the argument list. I can see why the syntax is as it is, there's no really good alternative, but it feels wrong.
There's no clear 'functional programming' benefit to the property syntax, either. Even though the syntax implies that Dist is logically some kind of a Func<double>, I still won't be able to write Func<double> f = Dist, because Dist is of type double.
I'm also concerned about the idea (only 'planned' for now, I believe) for the same syntax for methods which take arguments, though. That feels like it risks pushing developers into the pit of failure by encouraging them to write public methods that contain no parameter validation logic, just evaluation of a return value. It also means that the instant you want to add more complexity to a method you're forced to completely rewrite it in the conventional curly brace syntax.
Then there's the halfway world of using the syntax for a method which takes no arguments:
public double Dist() => Sqrt(X * X + Y * Y);
where now I can write Func<double> f = Dist...
- louthy 13y agoAlthough I 100% agree with you on the expression based properties, the argument against that would be that it doesn't follow the norms for the language as a whole. First class functions are definitely an afterthought for C#, so making this a Func<double> would go against that I reckon: public double Dist => Sqrt(X * X + Y * Y); > I'm also concerned about the idea (only 'planned' for now, I believe) for the same syntax for methods which take arguments, though. Where's this? I must have missed it. Or could you show what it would look like?
- jameshart 13y agoWell note that the feature is described as "expression-bodied members", not "expression-bodied readonly properties". In Mads's "Future of C#" presentation where most of these features were first floated, he described using the syntax for declaring methods as well as properties: public Point Move(int dx, int dy) => new Point(X + dx, Y + dy); Which is logically consistent with the property syntax, but as I say, I have my reservations about it.