The stately := operator feature proposal

Diggory diggsey at googlemail.com
Wed May 29 18:18:50 PDT 2013


On Thursday, 30 May 2013 at 00:20:00 UTC, MrzlganeE wrote:
> Hi,
>
> 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.
>
> Please consider it.
>
> - MrzlganeE


You can do this:

template Math(string code) {
     enum Math = transform(code); // Do whatever CTFE transforms 
you want
}

mixin Math!q{
     x := 1;
     y := 2;

     writeln(x*y);
};

Although it will be a lot easier when std.regex works at compile 
time...


More information about the Digitalmars-d mailing list