Jump to page: 1 24  
Page
Thread overview
foreach syntax
Jun 29, 2012
Namespace
Jun 29, 2012
Matthias Walter
Jun 29, 2012
bearophile
Jun 29, 2012
Timon Gehr
Jun 29, 2012
Dejan Lekic
Jun 29, 2012
Timon Gehr
Jun 29, 2012
bearophile
Jun 29, 2012
Timon Gehr
Jun 29, 2012
Timon Gehr
Jun 29, 2012
bearophile
Jun 29, 2012
Timon Gehr
Jun 29, 2012
Jonathan M Davis
Jun 29, 2012
Jonathan M Davis
Jun 29, 2012
Namespace
Jun 29, 2012
Timon Gehr
Jun 29, 2012
Namespace
Jun 29, 2012
Namespace
Jun 29, 2012
David
Jun 29, 2012
Namespace
Jun 29, 2012
Tobias Pankrath
Jun 29, 2012
Namespace
Jun 29, 2012
Namespace
Jun 29, 2012
Timon Gehr
Jun 29, 2012
Timon Gehr
Jun 29, 2012
Namespace
Jun 29, 2012
Timon Gehr
Jun 29, 2012
Namespace
Jun 29, 2012
Timon Gehr
Jun 29, 2012
Namespace
Jun 29, 2012
Timon Gehr
Jun 29, 2012
Namespace
Jun 29, 2012
Jonathan M Davis
Jun 29, 2012
Namespace
Jun 29, 2012
Timon Gehr
Jun 29, 2012
Roman D. Boiko
Jun 29, 2012
Timon Gehr
Jun 29, 2012
Jonathan M Davis
June 29, 2012
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.
June 29, 2012
On 06/29/2012 12:47 PM, 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.
> 

I suppose it is because the usual 'for' loop is a relative of 'foreach'. And there we (and the C world) uses ';'.
June 29, 2012
Namespace:
> A friend of mine ask me why D's foreach isn't like C#

In D you often omit the type:

foreach (val; arr) {

Using "in" is better for the human programmers. But D is largely designed to make D compilers too happy. I think Walter said that the semicolon was preferred because it simplifies the compiler/compilation a little.

Bye,
bearophile
June 29, 2012
On Fri, 29 Jun 2012 12:47:28 +0200, 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.


Because
1) "in" is a D keyword
2) D has even shorter syntax: foreach(val; arr) {}


-- 
Dejan Lekic
  mailto:dejan.lekic(a)gmail.com
  http://dejan.lekic.org
June 29, 2012
On 06/29/2012 12:47 PM, 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) {
>

foreach(val; arr) {

> and not
> foreach (int val in arr) {
>
> which it is more intuitive.
>

To someone coming from C#, yes.

> I could give him no clever answer to, so maybe someone here knows the
> reasons.

Just because. This does not matter.
June 29, 2012
Timon Gehr:

> Just because. This does not matter.

Now and then I write foreach(x;y;data) or foreach(x,y,data) or foreach(x;y,data). "in" avoids some of those little mistakes.

Bye,
bearophile
June 29, 2012
On 06/29/2012 01:01 PM, bearophile wrote:
> Namespace:
>> A friend of mine ask me why D's foreach isn't like C#
>
> In D you often omit the type:
>
> foreach (val; arr) {
>
> Using "in" is better for the human programmers.

Certainly not. (except if 'human' means 'python' or 'C#'.)
It is just as good as ';'.

> But D is largely
> designed to make D compilers too happy. I think Walter said that the
> semicolon was preferred because it simplifies the compiler/compilation a
> little.

It does not. Parsing the statements just about the same. In fact, only
2 lines in the DMD parser implementation need to be changed (slightly!)
to implement the 'in' syntax instead.
June 29, 2012
On 06/29/2012 04:16 PM, bearophile wrote:
> Timon Gehr:
>
>> Just because. This does not matter.
>
> Now and then I write foreach(x;y;data)

error: found ';' when expecting ')'

> or foreach(x,y,data) or

error: found ')' when expecting ';'

> foreach(x;y,data).

error: undefined identifier y

> "in" avoids some of those little mistakes.
>

foreach(x in y,data)
June 29, 2012
On 06/29/2012 04:23 PM, Timon Gehr wrote:
> ...
>> foreach(x;y,data).
>
> error: undefined identifier y
>

BTW, it would certainly be better if this didn't actually pass the parser.
June 29, 2012
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 ";".

Bye,
bearophile
« First   ‹ Prev
1 2 3 4