Thread overview | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 05, 2016 Why doesn't std.variant.visit automatically call the provided delegates? | ||||
---|---|---|---|---|
| ||||
``` 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); } ``` |
November 05, 2016 Re: Why doesn't std.variant.visit automatically call the provided delegates? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nemanja Boric | 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; }); |
November 05, 2016 Re: Why doesn't std.variant.visit automatically call the provided delegates? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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:
>> [...]
>
> 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:
>
>> [...]
Oh, God. Thanks, Adam, all clear!
Thanks!
|
November 05, 2016 Re: Why doesn't std.variant.visit automatically call the provided delegates? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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:
>> // 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; });
That's really confusing. I've used D for quite a while, and didn't know that. Admittedly I doubt I've ever tried () => { }, but given languages like C# which this syntax was partially taken from(?), that behaviour is very unexpected. That feels like it should be a compiler warning.
|
November 05, 2016 Re: Why doesn't std.variant.visit automatically call the provided delegates? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kapps | 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.
|
November 06, 2016 Re: Why doesn't std.variant.visit automatically call the provided delegates? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kapps | On Sat, 05 Nov 2016 20:15:14 +0000, Kapps wrote:
> Admittedly I doubt I've ever tried () => { }, but given languages
> like C# which this syntax was partially taken from(?)
D had delegates of that form years before C# had short-form delegates. Otherwise it would likely have followed suit.
|
November 06, 2016 Re: Why doesn't std.variant.visit automatically call the provided delegates? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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:
>> 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.
Have you discussed this with Andrei or Walter? It would be a good change.
|
November 06, 2016 Re: Why doesn't std.variant.visit automatically call the provided delegates? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Wright | On Sunday, 6 November 2016 at 00:19:57 UTC, Chris Wright wrote:
> On Sat, 05 Nov 2016 20:15:14 +0000, Kapps wrote:
>> Admittedly I doubt I've ever tried () => { }, but given languages
>> like C# which this syntax was partially taken from(?)
>
> D had delegates of that form years before C# had short-form delegates. Otherwise it would likely have followed suit.
The => comes exactly from C# and was introduced probably less than 2 years ago.
|
November 07, 2016 Re: Why doesn't std.variant.visit automatically call the provided delegates? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kapps | 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 () => { }, but given languages like C# which this syntax was partially 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).
|
November 08, 2016 Re: Why doesn't std.variant.visit automatically call the provided delegates? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On 11/05/2016 04:22 PM, Adam D. Ruppe wrote:
> 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.
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.
|
Copyright © 1999-2021 by the D Language Foundation