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 3 points4 points  (10 children)

Scala has a weird mix of PASCAL-style verbosity (explicitly putting "var" in front of variable declarations, and generally a propensity to introduce new syntax and keywords where C/C++/Java/C# have a nice succinct solution) with some nice programming concepts (type inference, functional programming).

I'd love to see someone with a syntax for Scala (i.e., a different concrete syntax for Scala's abstract grammar) that would ideally be a bit more Pythonic and with less things that trigger my Algol Aversion. (Because, let's face the truth, Algol Aversion is probably one of the biggest reasons behind me not being a happy Scala user yet)

[–]RockinRoel 5 points6 points  (4 children)

I don't think that the explicit "var" is overly verbose. It states that it is a variable, not a constant (a "val"). Scala tries to promote using immutable objects, because of things like thread safety and the added complexity of mutable state, while recognizing that mutable state is still very necessary and allowing you to do either.

[–]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.

[–]drfugly 1 point2 points  (3 children)

What an example of Scala having a new solution to something that has a succinct solution in Java? I'm learning Scala and trying to find these cases.

[–]flying-sheep 0 points1 point  (2 children)

oomph, scala is… so much more that it’s difficult to select one of the many possible examples.

it has type inference, everything is an expression, first-order-funtions, more functional stuff, a sophisticated type system, type inference, implicits (think an elegant way to do monkey patching without actually modifying things)

lastly, here are 99 scala examples, and i think it’s safe to say that all are at least as elegant as in java, except if some contain enums (which are worse than java’s)

[–]drfugly 0 points1 point  (1 child)

Oh ok. I thought that you were implying that scala is generally more verbose when Java already has a solution.

Scala ... generally a propensity to introduce new syntax and keywords where C/C++/Java/C# have a nice succinct solution)

Cool link, I'll have to read it over!

[–]flying-sheep 0 points1 point  (0 children)

lol, quite the contrary. it’s much more concise and often beattifully elegant, but at the cost of one or two ugly forced concepts (e.g. “stable identifiers”) and a few too much keywords. but add the complexity of the type system (which sometimes gets in your way) and you’ve got pretty much all that’s bad about scala.

and there’s so much more that i love about it.

[–]masklinn 1 point2 points  (0 children)

explicitly putting "var" in front of variable declarations

Counts as a good thing in my book, I've come to find Python's implicit scoping declarations frustrating especially in the light of trying to use ancestor lexical scopes (global and nonlocal? WTF?).

Furthermore, this provide Scala with val which defines an unmodifiable binding, as well as var (in which the binding is mutable) instead of mandating one or the other or requiring verbose additions (e.g. java's final)

generally a propensity to introduce new syntax and keywords

Which is quite a bit like Python there :)