digitalmars.D - Lambda syntax for methods and functions
- bearophile (12/12) Dec 07 2013 From this post:
- MattCoder (4/8) Dec 07 2013 Maybe It's just me, but on the example above I don't see too much
- MattCoder (15/15) Dec 07 2013 Sorry I forgot one thing:
- bearophile (20/23) Dec 07 2013 In D:
- MattCoder (4/22) Dec 07 2013 Interesting, I didn't know about this. Thanks for the tip.
- Timon Gehr (3/5) Dec 07 2013 IIRC Don said this shouldn't work since the context is not actually
- bearophile (7/9) Dec 07 2013 I remember Don saying similar things on some other usages of
- monarch_dodra (8/16) Dec 09 2013 I didn't know you could do that, neat.
- Jesse Phillips (3/13) Dec 07 2013 You've pretty much just summed up the argument not to introduce
- Marco Leise (14/29) Dec 07 2013 I think it is a good middle ground for predicates where I
- Jesse Phillips (3/5) Dec 08 2013 I think you mean "the verbose D syntax:"
- Marco Leise (5/11) Dec 08 2013 :(
- Craig Dillabaugh (11/16) Dec 09 2013 Maybe it is because I am a fast typer and a slow thinker, but I
- bearophile (20/20) Dec 08 2013 The relative Reddit thread explains another small feature of the
- Paulo Pinto (9/29) Dec 09 2013 Another intesting thing for D community to know is that it will
- bearophile (4/5) Dec 10 2013 Better explanations:
- Joshua Niehus (3/9) Dec 10 2013 similar to dart, my 2nd favorite lang :)
From this post: http://adamralph.com/2013/12/06/ndc-diary-day-3/?1 public Point Move(int dx, int dy) => new Point(X + dx, Y + dy); That means: public Point Move(int dx, int dy) { return new Point(X + dx, Y + dy); } Even Ada2012 has a similar syntax. I think it's worth having in D. The ER: https://d.puremagic.com/issues/show_bug.cgi?id=7176 Bye, bearophile
Dec 07 2013
On Saturday, 7 December 2013 at 17:29:43 UTC, bearophile wrote:public Point Move(int dx, int dy) => new Point(X + dx, Y + dy); That means: public Point Move(int dx, int dy) { return new Point(X + dx, Y + dy); }Maybe It's just me, but on the example above I don't see too much improvement: 1 word off ("return"), and "=>" instead of brackets.
Dec 07 2013
Sorry I forgot one thing: On this code: public Point Move(int dx, int dy) { return new Point(X + dx, Y + dy); } It would be too much trouble if the language automatically return the last statement if this was the same type of the method? For example: public Point Move(int dx, int dy){ new Point(X + dx, Y + dy); } The return keyword wouldn't needed to be declared, because the statement is a Point type which is the same type that method returns. Something like for example what happens in LISP, which returns the last statement by default.
Dec 07 2013
MattCoder:Maybe It's just me, but on the example above I don't see too much improvement: 1 word off ("return"), and "=>" instead of brackets.In D: Point move(in int dx, in int dy) pure nothrow { return Point(X + dx, Y + dy); } Point move(in int dx, in int dy) pure nothrow => Point(X + dx, Y + dy); It saves you almost 10% of typing, and makes more visible that move is an expression. If you use UFCS chains a lot, many functions become small and they sometimes contain just an expression. Currently in D you can write: enum move = (in int dx, in int dy) pure nothrow => Point(X + dx, Y + dy); But here you can't specify the return value, this sometimes is a problem, because you miss the implicit cast at the return point (char[] -> string in pure functions, char -> dchar, Point -> const(Point), etc). Bye, bearophile
Dec 07 2013
On Saturday, 7 December 2013 at 18:13:06 UTC, bearophile wrote:MattCoder:Well,I think you have a point here.Maybe It's just me, but on the example above I don't see too much improvement: 1 word off ("return"), and "=>" instead of brackets.In D: Point move(in int dx, in int dy) pure nothrow { return Point(X + dx, Y + dy); } Point move(in int dx, in int dy) pure nothrow => Point(X + dx, Y + dy); It saves you almost 10% of typing, and makes more visible that move is an expression. If you use UFCS chains a lot, many functions become small and they sometimes contain just an expression.Currently in D you can write: enum move = (in int dx, in int dy) pure nothrow => Point(X + dx, Y + dy);Interesting, I didn't know about this. Thanks for the tip. Matheus.
Dec 07 2013
On 12/07/2013 07:13 PM, bearophile wrote:Currently in D you can write: enum move = (in int dx, in int dy) pure nothrow => Point(X + dx, Y + dy);IIRC Don said this shouldn't work since the context is not actually known at compile time.
Dec 07 2013
Timon Gehr:IIRC Don said this shouldn't work since the context is not actually known at compile time.I remember Don saying similar things on some other usages of 'enum'. If all this needs to become accepts-invalid errors, then dmd 2.065 seems a good moment to fix this messy situation. Suggestions from Walter/Andrei are welcome. Bye, bearophile
Dec 07 2013
On Saturday, 7 December 2013 at 20:23:29 UTC, Timon Gehr wrote:On 12/07/2013 07:13 PM, bearophile wrote:I didn't know you could do that, neat. It also means you can do it for implicit template, I suppose. You can't do it for declaring a function, but I suppose it works for declaring a lambda, and storing it in a enum: enum moveRight(int DX) = (in int dx) pure nothrow => dx + DX;Currently in D you can write: enum move = (in int dx, in int dy) pure nothrow => Point(X + dx, Y + dy);IIRC Don said this shouldn't work since the context is not actually known at compile time.I only see this being a problem if the lambda actually needs access to the context.
Dec 09 2013
On Saturday, 7 December 2013 at 17:43:46 UTC, MattCoder wrote:On Saturday, 7 December 2013 at 17:29:43 UTC, bearophile wrote:You've pretty much just summed up the argument not to introduce lambda syntax. It has been proven wrong.public Point Move(int dx, int dy) => new Point(X + dx, Y + dy); That means: public Point Move(int dx, int dy) { return new Point(X + dx, Y + dy); }Maybe It's just me, but on the example above I don't see too much improvement: 1 word off ("return"), and "=>" instead of brackets.
Dec 07 2013
Am Sat, 07 Dec 2013 21:21:03 +0100 schrieb "Jesse Phillips" <Jesse.K.Phillips+D gmail.com>:On Saturday, 7 December 2013 at 17:43:46 UTC, MattCoder wrote:I think it is a good middle ground for predicates where I don't like to use strings mixins instead of plain expressions: "a > b" But the verbose D syntax is just too distracting: bool function(T a, T b) { return a > b; }) lambdas on the other hand save a lot of typing and have first class language support: (a, b) => a > b If some day we can omit the parameter list like in some other language, I wouldn't mind. -- MarcoOn Saturday, 7 December 2013 at 17:29:43 UTC, bearophile wrote:You've pretty much just summed up the argument not to introduce lambda syntax. It has been proven wrong.public Point Move(int dx, int dy) => new Point(X + dx, Y + dy); That means: public Point Move(int dx, int dy) { return new Point(X + dx, Y + dy); }Maybe It's just me, but on the example above I don't see too much improvement: 1 word off ("return"), and "=>" instead of brackets.
Dec 07 2013
On Sunday, 8 December 2013 at 06:58:59 UTC, Marco Leise wrote:But the verbose D syntax is just too distracting: bool function(T a, T b) { return a > b; })I think you mean "the verbose D syntax:" (a, b) { return a > b; }
Dec 08 2013
Am Sun, 08 Dec 2013 18:15:40 +0100 schrieb "Jesse Phillips" <Jesse.K.Phillips+D gmail.com>:On Sunday, 8 December 2013 at 06:58:59 UTC, Marco Leise wrote::( -- MarcoBut the verbose D syntax is just too distracting: bool function(T a, T b) { return a > b; })I think you mean "the verbose D syntax:" (a, b) { return a > b; }
Dec 08 2013
On Sunday, 8 December 2013 at 17:15:42 UTC, Jesse Phillips wrote:On Sunday, 8 December 2013 at 06:58:59 UTC, Marco Leise wrote:Maybe it is because I am a fast typer and a slow thinker, but I kind of like somewhat verbose syntax (maybe not Java level verbose, but still verbose). My brain can parse a statement with words in it much faster than it can a mess of brackets and the like. I am a human, not a compiler. Heck if I was designing my own programming language I'd likely have something like: lambda(a,b) { return a > b; } Plus I would have 'and' and 'or' instead of && and ||, just because that has always bugged me.But the verbose D syntax is just too distracting: bool function(T a, T b) { return a > b; })I think you mean "the verbose D syntax:" (a, b) { return a > b; }
Dec 09 2013
The relative Reddit thread explains another small feature of the http://www.reddit.com/r/programming/comments/1sbkxl/a_quick_highlight_of_upcoming_c_language_changes/cdwedrh If I understand that, it's similar to allowing D code like this: bool foo(out int x) { x = 10; return true; } void main() { if (foo(int y)) y.writeln; } Currently in D you have to write: void main() { int y; if (foo(y)) y.writeln; } Bye, bearophile
Dec 08 2013
On Sunday, 8 December 2013 at 11:23:48 UTC, bearophile wrote:The relative Reddit thread explains another small feature of http://www.reddit.com/r/programming/comments/1sbkxl/a_quick_highlight_of_upcoming_c_language_changes/cdwedrh If I understand that, it's similar to allowing D code like this: bool foo(out int x) { x = 10; return true; } void main() { if (foo(int y)) y.writeln; } Currently in D you have to write: void main() { int y; if (foo(y)) y.writeln; } Bye, bearophileAnother intesting thing for D community to know is that it will gain a new JIT. http://blogs.msdn.com/b/dotnet/archive/2013/11/18/ryujit-net-jit-compiler-ctp1-faq.aspx And besides Windows Phone 8, the additional option to generate native code for other targets as well was briefly mentioned during the VS 2013 keynote as you can see from the comments. -- Paulo
Dec 09 2013
http://adamralph.com/2013/12/06/ndc-diary-day-3/?1Better explanations: http://damieng.com/blog/2013/12/09/probable-c-6-0-features-illustrated Bye, bearophile
Dec 10 2013
On Saturday, 7 December 2013 at 17:29:43 UTC, bearophile wrote:Even Ada2012 has a similar syntax. I think it's worth having in D. The ER: https://d.puremagic.com/issues/show_bug.cgi?id=7176 Bye, bearophilesimilar to dart, my 2nd favorite lang :) https://www.dartlang.org/articles/idiomatic-dart/
Dec 10 2013