June 29, 2012
On 06/29/2012 04:34 PM, bearophile wrote:
> Timon Gehr:
>
>> foreach(x in y,data)
>
> There is no way to avoid all possible mistakes, but it's easier to
> mistake a ";" for a ",", than mistake a "in" for a ",".
>

I don't think optimizing the grammar for error cases that are not even
compilable code is worthwhile at all.

> "in" is used for this purpose in Python, C#, and probably other
> languages because it's more readable

Probably it is used in those languages because it resembles
mathematical notation, or let var in exp. I doubt there is anything
deep behind it.

> than an arbitrary symbol like ";".
>

Probably, but when I read code, I don't read the ';', it is just a
separator and helps orientation. Making separators 'readable' is imho a
non-goal. But YMMV. Patching the parser so that it accepts both forms
takes 20 seconds.
June 29, 2012
On Friday, June 29, 2012 16:34:33 bearophile wrote:
> Timon Gehr:
> > foreach(x in y,data)
> 
> There is no way to avoid all possible mistakes, but it's easier to mistake a ";" for a ",", than mistake a "in" for a ",".
> 
> "in" is used for this purpose in Python, C#, and probably other languages because it's more readable than an arbitrary symbol like ";".

And in Java, they use : rather than ; or in. This is completely subjective. I see _zero_ benefit in using in over ;. ; is nicely consistent with for, and is probably why foreach in D uses it. But regardless, it's purely a subjective choice. Walter picked what he picked. C# and python picked what they picked. Java picked what they picked. I really don't think that you can objectively argue that one is better than the other - not unless one is objectively easier to write the grammar for. Anything involving whether a person thinks in is or ; is easier to read or will cause fewer mistakes or whatnot is going to be purely subjective.

- Jonathan M Davis
June 29, 2012
On Friday, June 29, 2012 12:47:28 Namespace wrote:
> A friend of mine ask me why D's foreach isn't like C#
> 
> Means, why is it like
> int[] arr = [1, 2, 3];
> 
> foreach (int val; arr) {
> 
> and not
> foreach (int val in arr) {
> 
> which it is more intuitive.
> 
> I could give him no clever answer to, so maybe someone here knows the reasons.

Probably because for uses ; rather than in. But it's a purely subjective language design decision. Walter could have used : like Java does, and it would have worked just as well. He decided to go with ;. On the whole, we was basing D on C++, not C#, so he didn't copy what C# was doing syntactically for anything AFAIK. Lots of stuff like this is going to vary from language to language, and often the decisions are arbitrary and have nothing to do with what other languages did or are based completely on the lanugage designer's personal preferences. If there's no technical reason for going with one over the other (and I'm not aware of one in this case), then it's up to the language designer to make a choice on it which is going to be completely subjective.

- Jonathan M Davis
June 29, 2012
It wasn't my intention to start a syntax war. :D
But i must agree, that "in" is a lot more readable as ";". Event ":" ist more readable as ";". But i just would know the real reason to this decision. Tanks to all. :)
But why correct a few guys here my code?
foreach (int val; is the same as foreach(val; except that i like to write which type "val" is. Is that against the D nature or what?

P.S.: Which line must be changed to allow the "in2 syntax?
Just out of pure interest. Thanks.
June 29, 2012
On 06/29/2012 06:09 PM, Namespace wrote:
> It wasn't my intention to start a syntax war. :D
> But i must agree, that "in" is a lot more readable as ";". Event ":" ist
> more readable as ";". But i just would know the real reason to this
> decision. Tanks to all. :)
> But why correct a few guys here my code?

It was more of a suggestion than a correction.

> foreach (int val; is the same as foreach(val; except that i like to
> write which type "val" is. Is that against the D nature or what?
>

It is redundant.

> P.S.: Which line must be changed to allow the "in2 syntax?
> Just out of pure interest. Thanks.

Search for TOKforeach in parse.c and change the TOKsemicolon's around there to TOKin's. If you want to allow both, that should be
straightforward as well.
June 29, 2012
> It is redundant.

But informative.

> Search for TOKforeach in parse.c and change the TOKsemicolon's around there to TOKin's. If you want to allow both, that should be
> straightforward as well.

I will try.


June 29, 2012
You mean line 3817 change to
if (t->value == TOKcomma || t->value == TOKsemicolon || t->value == TOKin) ?
But i have to rebuild dmd or not? I'm a windows user and I never build dmd on my own.
June 29, 2012
You mean just change line 3817 to
if (t->value == TOKcomma || t->value == TOKsemicolon || t->value == TOKin) ?
But know i have to rebuild dmd (i use windows), and i never did this before. :/
June 29, 2012
Am 29.06.2012 18:23, schrieb Namespace:
> You mean line 3817 change to
> if (t->value == TOKcomma || t->value == TOKsemicolon || t->value ==
> TOKin) ?
> But i have to rebuild dmd or not? I'm a windows user and I never build
> dmd on my own.
Yes, you have to recompile DMD

June 29, 2012
On Friday, 29 June 2012 at 16:27:23 UTC, Namespace wrote:
> You mean just change line 3817 to
> if (t->value == TOKcomma || t->value == TOKsemicolon || t->value == TOKin) ?
> But know i have to rebuild dmd (i use windows), and i never did this before. :/

Which is easy. I write a guide as soon as time permits.