Something Go and Scala syntax

bearophile bearophileHUGS at lycos.com
Thu Dec 30 02:55:13 PST 2010


Time ago with other people I have asked to replace the keyword "invariant" with "immutable" to create a run-time constant. But that wasn't the best choice because now in my functions I am using many immutable values; writing "immutable" often is boring and makes code longer. This is normal D2 code:

void foo(immutable int y) {
    immutable x = 5;
    if (i > x) {
        writeln(x);
    }
    if (i > x)
        writeln(x);
}



So using "val" (abbreviation for "value") as in Scala seems better to me:

void foo(val int y) {
    val x = 5;
    if (i > x) {
        writeln(x);
    }
    if (i > x)
        writeln(x);
}


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:

void foo(immutable int y) {
    x := 5;
    if i > x {
        writeln(x);
    }
    if (i > x)
        writeln(x);    
    if i > x { // {} become necessary if you remove ()
        writeln(x);
    }
}

Bye,
bearophile


More information about the Digitalmars-d mailing list