February 26, 2006 if(;){} opinion | ||||
---|---|---|---|---|
| ||||
Better 'fess up front: the intent of this post is to once and for all murder, pulverize and extradite the new if construct. Consider: if (m; std.regexp.search("abcdef", "b(c)d")) { writefln("[%s]", m.pre); // prints [a] writefln("[%s]", m.post); // prints [ef] writefln("[%s]", m.match(0)); // prints [bcd] writefln("[%s]", m.match(1)); // prints [c] writefln("[%s]", m.match(2)); // prints [] } Flauting this around has shown that experienced programmers have a hard time figuring out what is going on here. Consider: if (m; std.regexp.search("abcdef", "b(c)d")) Most everybody take it for granted that here is a typo, the '(m;' must be the result of a sloppy copy-paste. And in the previous case the theories ranged from all kinds of behind-the-scenes magic. Now consider: if (Regexp m = std.regexp.search("abcdef", "b(c)d")) { writefln("[%s]", m.pre); // prints [a] writefln("[%s]", m.post); // prints [ef] writefln("[%s]", m.match(0)); // prints [bcd] writefln("[%s]", m.match(1)); // prints [c] writefln("[%s]", m.match(2)); // prints [] } Flaunting this around (to both the original programmers, and also to virgin victims), gave the same, _immediate_ comment from everybody: "Ah, that's neat!" |
February 26, 2006 Re: if(;){} opinion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Georg Wrede | "Georg Wrede" <georg@nospam.org> wrote in message news:4401ECE3.4030207@nospam.org... > Better 'fess up front: the intent of this post is to once and for all murder, pulverize and extradite the new if construct. > > Consider: > > if (m; std.regexp.search("abcdef", "b(c)d")) > { > writefln("[%s]", m.pre); // prints [a] > writefln("[%s]", m.post); // prints [ef] > writefln("[%s]", m.match(0)); // prints [bcd] > writefln("[%s]", m.match(1)); // prints [c] > writefln("[%s]", m.match(2)); // prints [] > } > > Flauting this around has shown that experienced programmers have a hard time figuring out what is going on here. > > Consider: > > if (m; std.regexp.search("abcdef", "b(c)d")) > > Most everybody take it for granted that here is a typo, the '(m;' must be the result of a sloppy copy-paste. > > And in the previous case the theories ranged from all kinds of behind-the-scenes magic. > > Now consider: > > if (Regexp m = std.regexp.search("abcdef", "b(c)d")) > { > writefln("[%s]", m.pre); // prints [a] > writefln("[%s]", m.post); // prints [ef] > writefln("[%s]", m.match(0)); // prints [bcd] > writefln("[%s]", m.match(1)); // prints [c] > writefln("[%s]", m.match(2)); // prints [] > } > > Flaunting this around (to both the original programmers, and also to virgin victims), gave the same, _immediate_ comment from everybody: "Ah, that's neat!" I can see where the new syntax came from: foreach(Foo x; a) But I'll agree with you that it doesn't seem to make much sense in the context of an if statement. I remember a while ago someone posting a suggestion that the ability to declare and initialize variables in conditional statements be extended from just for (and foreach) to all kinds of statements, such as your suggestion. I do like the "if(type ident = expr)" syntax. |
February 26, 2006 Re: if(;){} opinion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Georg Wrede | "Georg Wrede" <georg@nospam.org> wrote in message news:4401ECE3.4030207@nospam.org... > Now consider: > > if (Regexp m = std.regexp.search("abcdef", "b(c)d")) > { > writefln("[%s]", m.pre); // prints [a] > writefln("[%s]", m.post); // prints [ef] > writefln("[%s]", m.match(0)); // prints [bcd] > writefln("[%s]", m.match(1)); // prints [c] > writefln("[%s]", m.match(2)); // prints [] > } > > Flaunting this around (to both the original programmers, and also to virgin victims), gave the same, _immediate_ comment from everybody: "Ah, that's neat!" The idea behind m; was to be like foreach. What do the "victims" <g> think of: if (auto m = std.regexp.search("abcdef", "b(c)d")) ? |
February 26, 2006 Re: if(;){} opinion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Georg Wrede | Honestly, I must agree that I prefer:
if (auto m = foo())
Or even:
if (auto m; foo())
To:
if (m; foo())
Or *most certainly*:
if (m = foo())
We have types everywhere else in the language (although in places we can leave them off) so it doesn't seem strange here either. I can imagine wanting, even, to ASSERT that I'm getting an int from the function. If this changes to a class, my code probably won't work anymore anyway.
-[Unknown]
> Better 'fess up front: the intent of this post is to once and for all murder, pulverize and extradite the new if construct.
>
> Consider:
>
> if (m; std.regexp.search("abcdef", "b(c)d"))
> {
> writefln("[%s]", m.pre); // prints [a]
> writefln("[%s]", m.post); // prints [ef]
> writefln("[%s]", m.match(0)); // prints [bcd]
> writefln("[%s]", m.match(1)); // prints [c]
> writefln("[%s]", m.match(2)); // prints []
> }
>
> Flauting this around has shown that experienced programmers have a hard time figuring out what is going on here.
>
> Consider:
>
> if (m; std.regexp.search("abcdef", "b(c)d"))
>
> Most everybody take it for granted that here is a typo, the '(m;' must be the result of a sloppy copy-paste.
>
> And in the previous case the theories ranged from all kinds of behind-the-scenes magic.
>
> Now consider:
>
> if (Regexp m = std.regexp.search("abcdef", "b(c)d"))
> {
> writefln("[%s]", m.pre); // prints [a]
> writefln("[%s]", m.post); // prints [ef]
> writefln("[%s]", m.match(0)); // prints [bcd]
> writefln("[%s]", m.match(1)); // prints [c]
> writefln("[%s]", m.match(2)); // prints []
> }
>
> Flaunting this around (to both the original programmers, and also to virgin victims), gave the same, _immediate_ comment from everybody: "Ah, that's neat!"
|
February 26, 2006 Re: if(;){} opinion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | Walter Bright wrote:
> "Georg Wrede" <georg@nospam.org> wrote in message news:4401ECE3.4030207@nospam.org...
>
>>Now consider:
>>
>> if (Regexp m = std.regexp.search("abcdef", "b(c)d"))
>> {
>> writefln("[%s]", m.pre); // prints [a]
>> writefln("[%s]", m.post); // prints [ef]
>> writefln("[%s]", m.match(0)); // prints [bcd]
>> writefln("[%s]", m.match(1)); // prints [c]
>> writefln("[%s]", m.match(2)); // prints []
>> }
>>
>>Flaunting this around (to both the original programmers, and also to virgin victims), gave the same, _immediate_ comment from everybody: "Ah, that's neat!"
>
> The idea behind m; was to be like foreach. What do the "victims" <g> think of:
>
> if (auto m = std.regexp.search("abcdef", "b(c)d"))
Can't give a statistical answer at this hour, but with one of them I actually brought it up. He thought it would be no problem, even to somebody not familiar with the language from before.
|
February 26, 2006 Re: if(;){} opinion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On Sun, 26 Feb 2006 13:43:55 -0500, Walter Bright <newshound@digitalmars.com> wrote:
>
> "Georg Wrede" <georg@nospam.org> wrote in message
> news:4401ECE3.4030207@nospam.org...
>> Now consider:
>>
>> if (Regexp m = std.regexp.search("abcdef", "b(c)d"))
>> {
>> writefln("[%s]", m.pre); // prints [a]
>> writefln("[%s]", m.post); // prints [ef]
>> writefln("[%s]", m.match(0)); // prints [bcd]
>> writefln("[%s]", m.match(1)); // prints [c]
>> writefln("[%s]", m.match(2)); // prints []
>> }
>>
>> Flaunting this around (to both the original programmers, and also to
>> virgin victims), gave the same, _immediate_ comment from everybody: "Ah,
>> that's neat!"
>
> The idea behind m; was to be like foreach. What do the "victims" <g> think
> of:
>
> if (auto m = std.regexp.search("abcdef", "b(c)d"))
>
> ?
That's what I was thinking, and perhaps it can be generalized to allow a declaration in any expression; allowing:
while(auto foo = bar) { }
baz(auto Foo f = new Foo); // RAII, f being destructed when baz() returns.
etc
|
February 27, 2006 Re: if(;){} opinion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | Walter Bright wrote: > "Georg Wrede" <georg@nospam.org> wrote in message news:4401ECE3.4030207@nospam.org... >> Now consider: >> >> if (Regexp m = std.regexp.search("abcdef", "b(c)d")) >> { >> writefln("[%s]", m.pre); // prints [a] >> writefln("[%s]", m.post); // prints [ef] >> writefln("[%s]", m.match(0)); // prints [bcd] >> writefln("[%s]", m.match(1)); // prints [c] >> writefln("[%s]", m.match(2)); // prints [] >> } >> >> Flaunting this around (to both the original programmers, and also to virgin victims), gave the same, _immediate_ comment from everybody: "Ah, that's neat!" > > The idea behind m; was to be like foreach. What do the "victims" <g> think of: > > if (auto m = std.regexp.search("abcdef", "b(c)d")) > That makes a lot more sense. Although I prefer if(type variable, variable = expression, expression == something) {...} or if(auto variable = expression, expression == something) {...} Unfortunately it's currently not possible (at least don't know how to do it) to declare anything that 'permanent' with the expression-comma-expression -syntax. I guess the scope ends, when the next comma is encountered. It would be very nice to support the comma-notation like above inside if-, while- and for-statements and stop supporting the current implementation elsewhere. IMO it's pretty useless outside these three statements. There's also other use for commas (e.g. initializing multi-dimensional arrays). I've proposed this before since the syntax for dynamic arrays is a bit weak at the moment. -- Jari-Matti |
February 27, 2006 Re: if(;){} opinion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | > The idea behind m; was to be like foreach. What do the "victims" <g> think of:
>
> if (auto m = std.regexp.search("abcdef", "b(c)d"))
That's what I'm used to! And no need for any "new stuff" either. I prefer this way.
L.
|
February 27, 2006 Re: if(;){} opinion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Unknown W. Brackets | > if (m = foo())
This one is tricky, since the programmer might have meant "==", but an explicit declaration might take care of that, I mean, one would be forced to add the type or "auto".
Talking of which: when is that "auto" ambiguity going to be resolved? (I still prefer "var" to "auto", but then again the RAII "auto" could use a rename too)
L.
|
February 27, 2006 Re: if(;){} opinion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lionello Lunesu | Lionello Lunesu schrieb:
> Talking of which: when is that "auto" ambiguity going to be resolved? (I still prefer "var" to "auto"
This would get my vote as well. "var" is far more intuitive and it would be similar to DScript. I don't think there's an obvious connection between "auto" and the data type of a variable.
Nils
|
Copyright © 1999-2021 by the D Language Foundation