digitalmars.D - Why doesn't std.variant.visit automatically call the provided
- Nemanja Boric (23/23) Nov 05 2016 ```
- Adam D. Ruppe (15/20) Nov 05 2016 It calls the function... which returns a delegate, which you
- Nemanja Boric (3/22) Nov 05 2016 Oh, God. Thanks, Adam, all clear!
- Kapps (6/30) Nov 05 2016 That's really confusing. I've used D for quite a while, and
- Adam D. Ruppe (5/6) Nov 05 2016 I'm now of the opinion that the {} delegates should be deprecated
- ixid (3/9) Nov 05 2016 Have you discussed this with Andrei or Walter? It would be a good
- Nick Sabalausky (4/9) Nov 08 2016 I didn't even know you could create a delegate with just {} and no
- Steven Schveighoffer (10/21) Nov 08 2016 Indeed:
- Steven Schveighoffer (5/10) Nov 08 2016 I created an enhancement request, as I didn't see any in the issue
- Chris Wright (3/5) Nov 05 2016 D had delegates of that form years before C# had short-form delegates.
- Jesse Phillips (3/9) Nov 06 2016 The => comes exactly from C# and was introduced probably less
- Nemanja Boric (6/11) Nov 07 2016 I was just going to say that after a weekend my mind on this is
``` import std.variant; import std.stdio; import std.exception; void main() { Algebraic!(int, string) variant; variant = 10; int x = 0; // Functions throws - uncomment and see // variant.tryVisit!( (string s) => enforce(false), // (int i) => enforce(false))(); // This - does nothing variant.visit!( (string s) => { enforce(false); x = 2; }, (int i) => { enforce(false); x = 3; })(); writeln("x = ", x); // This works as expected variant.visit!( (string s) => { x = 2; }(), (int i) => { x = 3; }())(); writeln("x = ", x); } ```
Nov 05 2016
On Saturday, 5 November 2016 at 08:27:49 UTC, Nemanja Boric wrote:// This - does nothing variant.visit!( (string s) => { enforce(false); x = 2; },It calls the function... which returns a delegate, which you never called. This is one of the most common mistakes people are making: {} in D is a delegate, and () => is a delegate, therefore () => {} is a delegate that returns a delegate... usually NOT what you want. What you wrote is equivalent to writing delegate() callback(string s) { return delegate() { enforce(false); x = 2; }; } Do not use the => syntax if there is more than one expression. You'll get what you want by simply leaving the => out:// This works as expected variant.visit!( (string s) { x = 2; }, (int i) { x = 3; });
Nov 05 2016
On Saturday, 5 November 2016 at 10:09:55 UTC, Adam D. Ruppe wrote:On Saturday, 5 November 2016 at 08:27:49 UTC, Nemanja Boric wrote:Oh, God. Thanks, Adam, all clear! Thanks![...]It calls the function... which returns a delegate, which you never called. This is one of the most common mistakes people are making: {} in D is a delegate, and () => is a delegate, therefore () => {} is a delegate that returns a delegate... usually NOT what you want. What you wrote is equivalent to writing delegate() callback(string s) { return delegate() { enforce(false); x = 2; }; } Do not use the => syntax if there is more than one expression. You'll get what you want by simply leaving the => out:[...]
Nov 05 2016
On Saturday, 5 November 2016 at 10:09:55 UTC, Adam D. Ruppe wrote:On Saturday, 5 November 2016 at 08:27:49 UTC, Nemanja Boric wrote:That's really confusing. I've used D for quite a while, and didn't know that. Admittedly I doubt I've ever tried () => { }, from(?), that behaviour is very unexpected. That feels like it should be a compiler warning.// This - does nothing variant.visit!( (string s) => { enforce(false); x = 2; },It calls the function... which returns a delegate, which you never called. This is one of the most common mistakes people are making: {} in D is a delegate, and () => is a delegate, therefore () => {} is a delegate that returns a delegate... usually NOT what you want. What you wrote is equivalent to writing delegate() callback(string s) { return delegate() { enforce(false); x = 2; }; } Do not use the => syntax if there is more than one expression. You'll get what you want by simply leaving the => out:// This works as expected variant.visit!( (string s) { x = 2; }, (int i) { x = 3; });
Nov 05 2016
On Saturday, 5 November 2016 at 20:15:14 UTC, Kapps wrote:That feels like it should be a compiler warning.I'm now of the opinion that the {} delegates should be deprecated (instead use () {} delegates)... this comes up a lot and there's a few other places too where it is a pain... and it isn't that worth keeping imo.
Nov 05 2016
On Saturday, 5 November 2016 at 20:22:13 UTC, Adam D. Ruppe wrote:On Saturday, 5 November 2016 at 20:15:14 UTC, Kapps wrote:Have you discussed this with Andrei or Walter? It would be a good change.That feels like it should be a compiler warning.I'm now of the opinion that the {} delegates should be deprecated (instead use () {} delegates)... this comes up a lot and there's a few other places too where it is a pain... and it isn't that worth keeping imo.
Nov 05 2016
On 11/05/2016 04:22 PM, Adam D. Ruppe wrote:On Saturday, 5 November 2016 at 20:15:14 UTC, Kapps wrote:I didn't even know you could create a delegate with just {} and no parens. Kinda confusing since, normally, {} by itself creates a scope, not a delegate.That feels like it should be a compiler warning.I'm now of the opinion that the {} delegates should be deprecated (instead use () {} delegates)... this comes up a lot and there's a few other places too where it is a pain... and it isn't that worth keeping imo.
Nov 08 2016
On 11/8/16 11:31 AM, Nick Sabalausky wrote:On 11/05/2016 04:22 PM, Adam D. Ruppe wrote:Indeed: {int x = foo; return x;} // scope auto dg = {int x = foo; return x;} // delegate The requirements seem to be that you have to use the {} syntax as an expression instead of a block. omitting the initial parentheses seems like a really low benefit to having such confusing ambiguity. And of course, it leads to the horrible examples shown by OP. -SteveOn Saturday, 5 November 2016 at 20:15:14 UTC, Kapps wrote:I didn't even know you could create a delegate with just {} and no parens. Kinda confusing since, normally, {} by itself creates a scope, not a delegate.That feels like it should be a compiler warning.I'm now of the opinion that the {} delegates should be deprecated (instead use () {} delegates)... this comes up a lot and there's a few other places too where it is a pain... and it isn't that worth keeping imo.
Nov 08 2016
On 11/5/16 4:22 PM, Adam D. Ruppe wrote:On Saturday, 5 November 2016 at 20:15:14 UTC, Kapps wrote:I created an enhancement request, as I didn't see any in the issue tracker already. https://issues.dlang.org/show_bug.cgi?id=16672 -SteveThat feels like it should be a compiler warning.I'm now of the opinion that the {} delegates should be deprecated (instead use () {} delegates)... this comes up a lot and there's a few other places too where it is a pain... and it isn't that worth keeping imo.
Nov 08 2016
On Sat, 05 Nov 2016 20:15:14 +0000, Kapps wrote:Admittedly I doubt I've ever tried () => { }, but given languagesOtherwise it would likely have followed suit.
Nov 05 2016
On Sunday, 6 November 2016 at 00:19:57 UTC, Chris Wright wrote:On Sat, 05 Nov 2016 20:15:14 +0000, Kapps wrote:than 2 years ago.Admittedly I doubt I've ever tried () => { }, but given languagesdelegates. Otherwise it would likely have followed suit.
Nov 06 2016
On Saturday, 5 November 2016 at 20:15:14 UTC, Kapps wrote:That's really confusing. I've used D for quite a while, and didn't know that. Admittedly I doubt I've ever tried () => { }, taken from(?), that behaviour is very unexpected. That feels like it should be a compiler warning.I was just going to say that after a weekend my mind on this is that this behavior is no different than C's: if (x = 5) { } issue. It deserves at least warning (if you make a delegate that returns delegate that can't be called).
Nov 07 2016