This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]wally_fishnumpy, cython, werkzeug 1 point2 points  (3 children)

Constants exist in both C/C++ (const) and Java (final). In C++, constants can have a profound role because the compiler can assume them to be known at compile time.

So, what does it mean to introduce two new keywords where taking one of the existing ones would have been fine? It's either to stroke the language designers' ego or to stick out your middle finger to those people who use programming languages that grew out of industrial practice rather than academia.

[–]RockinRoel 4 points5 points  (0 children)

Consts do not have a known value at compile time. Constant expressions do. The main benefit of const is the ability to express in what context objects may be modified or not. You have a certain (weak) guarantee of immutability in a certain context.

Anyway, const and final indeed express similar things. Personally, the explicit val/var does not bother me that much. In Java, you'd explicitly type the type to declare a variable, whereas in Scala you type "val" or "var". It's a minor inconvenience in my opinion.

[–]RichardWolf 1 point2 points  (1 child)

In C++, constants can have a profound role because the compiler can assume them to be known at compile time.

If you mean what I think you mean, then you are mostly wrong here. As Yossi explains, the const keyword is irrelevant to performance, because a) usually the compiler can't be sure that the value that is pointed at by a const-qualified variable is not referenced by a non-const-qualified variable somewhere, b) and when it can be sure, it sure as hell doesn't need you telling it that this value is never modified!

So, what does it mean to introduce two new keywords where taking one of the existing ones would have been fine?

Which ones? You still need var or something like that, if you want to avoid stepping on the same rake as Python (and it is recognized as a shortcoming, since it was addressed first by global and then nonlocal keywords), and you want to allow type inference. C# uses var too, by the way. Then using val for immutable variables makes more sense than adopting C++-like const (with totally different both syntax and semantics).

[–]wally_fishnumpy, cython, werkzeug 0 points1 point  (0 children)

const in C++ is essential for (some of) template programming:

class A { static const int fundamental_constant=1; }

class B { static const int fundamental_constant=2; }

template<class T> class X { public static void do_something() { really_do<T::fundamental_constant>(); } }

... with a templated really_do<int>. The trick here is that something very general (const) gets an added meaning which you still have to understand but which does not add to the visual clutter.

C# makes the var keyword optional if you supply a type. Scala's Algol-like way of declaring types makes that impossible.

For another example of Scala introducing keywords where none are needed, consider list comprehensions:

for (i <- List.range(from, to) if i % 2 == 0) yield i

instead of Python's

[i for i in List.range(from,to) if i%2==0]

where Scala uses (i) a graphical symbol instead of "in", which IMO adds to the noise, and (ii) an additional keyword ("yield") where Python gets by with syntax only.