The stately := operator feature proposal

bearophile bearophileHUGS at lycos.com
Thu May 30 05:03:35 PDT 2013


MrzlganeE:

> Please consider a request for just one piece of syntax sugar in 
> D.
>
> In places where I write a bunch of short mathy code, I do not 
> want to use 'auto'. The := operator would allow to declare a 
> variable, deduce its type, and define its value.
>
> void main() {
>     x := 1;
>     y := 2.0;
>     z := x * y;
>     y = 3.0;
> }
>
> I do not want to see the word 'auto', because it has little to 
> do with the problem I am expressing. I find it distracting. 
> 'auto' suggests D's type system -- but I am just thinking of 
> the algorithm. I want to see less words about the type system, 
> and focus my eyes on the problem that I am solving.
>
> I realize that D is not about syntax sugar, but this is 
> different:
>
> - It is easily understood by computer scientists, and 
> mathematicians coming from many different backgrounds -- it's 
> not obscure like a perl syntax construct.
>
> - I would not write 'auto' while expressing an algorithm on a 
> whiteboard. But I may very well write :=
>
> - It has a historical record of use in old BNF-grammar 
> languages.
>
> - I think people would really *use* it, where they don't want 
> to use auto today.
>
> - It's not 'un-C-like' -- it is only not-like-C, because C 
> didn't support the feature. It is actually a natural fit!
>
>     for (i := 0; i < 24; i++) {
>     }
>
> - It is very easily remembered: If you've seen it once, you 
> know it.
>
> Today, I would rather write 'double y = 2.0;' than 'auto y = 
> 2.0;'
> But := would change that.

I like the ":=" syntax because an assignment is an operation very 
different from an equality ("="). But then I'd like equality to 
be expressed with a single equal sign "=", both as in Pascal.

Generally in a language it's bad to have two ore more obvious 
ways to do something. D is almost compatible with C, so it has 
several duplications in the ways to do things. Such duplication 
is justified only when it introduces some significant advantage. 
This idea introduces a syntax duplication for not enough gain. So 
I am against it.

Bye,
bearophile


More information about the Digitalmars-d mailing list