The stately := operator feature proposal

Tyler Jameson Little beatgammit at gmail.com
Thu Jun 6 09:06:43 PDT 2013


On Friday, 31 May 2013 at 00:57:33 UTC, Minas Mina wrote:
> I don't think this is useful.
>
> At least when I see "auto" in the code I immediately understand 
> what's going on, whereas with this proposal I have to double 
> check my code to see if it's ":=" or "=".

First off, I write a _lot_ of Go code, and I _love_ the := there. 
It makes things nice and simple, and it fits nicely into the rest 
of the Go syntax. However, I don't think it belongs in D because 
it changes the flow of the code.

The problem is where type specifiers are expected to go. In D 
(and most other C-like languages), types go before the 
identifiers:

     int x, y, z;

When scanning code, if I see a type identifier, I know it's 
declaring something. I immediately know the scope and all is well.

In Go, types go after the identifiers:

     func example(x, y, z int) {}

This is only broken by var|type, which are completely different 
expressions.

For Go, the := makes perfect sense, because when you read Go 
code, you expect the identifier first, then the type. In D 
however, nothing else (correct me if I'm wrong) has this syntax.

I have no problem with the := syntax, I just think it doesn't 
make syntactic sense. It subtly breaks the idioms of the 
language, all for very little gain.

I would be okay with type blocks, or the presented math {} block 
(which could do all sorts of new and exciting things) because 
that would fit more nicely into the language.

If the OP really wants this, he/she can easily write a 
pre-processor for D code that he/she uses on his/her own personal 
projects. A completely untested regex:

     rsync src compilable-source
     find compilable-source/ -name "*.d" -exec sed -i 
"s/\(\\w+\)\\s*:=/auto \1 =/g" {}+

There, feature done in two lines of shell...


More information about the Digitalmars-d mailing list