Something Go and Scala syntax

Stanislav Blinov blinov at loniir.ru
Thu Dec 30 04:23:47 PST 2010


30.12.2010 15:05, bearophile пишет:
> Peter Alexander:
>
>> but val is far too vague. It does not suggest that the value is immutable at all.
> In Scala you tag assignments with "val" or "var". "var" means variable, and "val" means value, it's not mutable. I agree that "immutable" is a bit more explicit than "val", but I think "val" is acceptable...
>

What's wrong with "in"? It's even one character shorter.

>>> An alternative is to use Go syntax, and use the Pascal-like ":=" to denote a value assignment (function signature can't use := ).
>>> Here there is another idea from Go syntax: if the "then" clause of the "if" uses {} then the () around the test can be omitted:
>> Maybe I'm missing something, but what problem is this solving?
> The same as "val". Instead of writing "immutable" you use ":=" (as in Go).
>
> I agree all this post is just about three bits of syntax sugar, it's not Earth shaking stuff :-) But while writing D2 code I have felt a bit of resistance in writing "immutable" all the time, and this is bad, because D has to *encourage* the programmer to use immutable values where mutables are not necessary. This is why I have suggested possible ways to shorten the syntax.
I'd say that the keyword shows the intent more clearly than this subtly 
different assignment operator. It's easier to spot and comprehend 
"immutable x = 5" than "x := 5". Besides, moving the role of type 
modifier from keyword to operator doesn't seem a very good idea, 
especially if the intent is only one special case.
Though I agree that frequent typing of "immutable" can be tedious, I 
often find it kind of fun, because I simply like the word :) Then again, 
"immutable" in only 3 chars longer than "double" or "string".


More information about the Digitalmars-d mailing list