Another syntactic under-performer

Chris R. Miller lordsauronthegreat at gmail.com
Mon Oct 13 21:03:32 PDT 2008


Bill Baxter wrote:
> The colon.  I think this may be another piece of syntax that could be
> better used.
> 
> In particular its use in goto and switch labels.  I've seen a few
> interesting proposals that wanted to use colons but which ran into
> troubles with ambiguity vs goto and switch labels.   But switch case
> labels are already inside a syntactically distinct structure, so it
> should be easy to change them to just about any syntax.   And the more
> generic labeled statements used for goto targets appear relatively
> infrequently (*), so hogging the colon just for those is wasteful.   A
> more verbose syntax would work fine for these.  Like maybe a context
> keyword on goto:   "goto(label) FINISHED"
> 
> With the colon freed up to use in another statement or expression it
> could be made part of a lambda syntax or something.
> 
> Just a thought.
> 
> --bb
> (* except in Walter's code  :-P  )

What about modifying foreach?  I may be unpopular for saying this, but 
use use of the semicolon in foreach is misleading.  The reason for using 
the semicolon in the for loop is to signify distinct statements.  It is 
for this reason that in Java 1.5 they used the colon in their foreach 
(aka the "new" for loop, they didn't bother adding the "each"), sorta 
like this:

ArrayList<int> IntList=new ArrayList<int>();

IntList.add(1);
IntList.add(2);
IntList.add(3);

for(int i:IntList) //
     System.out.println(i);

IMHO this is not misleading (but not ideal).  By contrast D's current 
foreach:

foreach(int i;IntList)
     printf("%d\n",i);

IMHO ideal would be more like this, using the "in" operator:

foreach(int i in IntList)

I'm unsure of possible precedence collisions inherent in that syntax, 
but on the bright side you could implement it alongside the existing 
syntax without problems.

I just wanted to float these ideas past, since it was somewhat on-topic.



More information about the Digitalmars-d mailing list