I wonder why they decided on these very mistakable names. Why not const/constant/cons/whatever else just as long it's distinguishable from each other?
I wonder why they decided on these very mistakable names. Why not const/constant/cons/whatever else just as long it's distinguishable from each other?
(I'm sure that this is just something you need to get used to, I'm merely trying to point out that this could be a silent typo/mistake)
Considering who authored the language, I am not surprised at all.
But this most likely is because Scala uses val/var.
That's nonsense.
1. You cannot implement immutable collections without `let` / `final`
2. The Java Memory Model has special visibility guarantees for `final` (which does propagate), making it really, really useful
3. Having the guarantee that a certain reference won't change is still useful even if the object referenced is a mutable ArrayList; e.g. ref = null
2. Java does a lot with the keyword 'final'. I guess here you're talking about the concurrency behaviors of it? Does it bother you that you can remove 'final' via reflection?
3. It's still useful (e.g. not needing to use yoda-style ifs in languages where you can assign inside an if expression Just In Case you forget an =), but not very. That's my whole point.
What `final` guarantees is that the variable will be initialized and visible (along with all its referenced objects) before the class constructor is finished. Without this guarantee you'd need `volatile` semantics or locks, which are more problematic.
2. yes, I'm talking about multi-threading; if the user removes "final" via reflection, or modifies final references via reflection, then it gets what he's asking for; but no, it does not bother me because I never do that and I stay away from libraries using reflection anyway.
val/var/const/whatever only describe the reference, not the value. It doesn't really make sense for that annotation to, say, swivel a collection between ImmutableList and MutableList.
Now, you might be right that it's confusing, this difference between reference mutability and value mutability. I see beginners struggle with it in Javascript's new let vs const all the time.
It does; if you don't use `let mut`, you can't mutate the variable at all, which includes the contents of a collection.
(Given that someone will mention RefCell if I don't, I'll add: "barring unusual trickery".)
let (mut x, y) = (1, 2);
x is mutable, y isn't.That is, let is the way that you introduce a new binding, always.
1 + f in modern Python results in a ValueError.
1) the compiler can then warn you when you violate your own declarations before the code is ever run. e.g. it can tell you that you've mutated something you said you didn't want to, or that you've taken the sum of an int and a list.
2) the compiler can guarantee certain things at compile time and thus eliminate the overhead of checking them at runtime. since a value in Python can be anything, before performing a string operation on a string the python runtime must first check that it is a string, while the runtime of a language like rust can assume that it is because that was guaranteed at compile time.
mut listOfLeftHandedOralHygienistsInKazakhstan = getThem()
// several lines later
listOfLeftHandedOralHygienistsInKazahkstan = getThemAgain()
You thought you were reassigning the mutable variable, but you actually created a different immutable variable.This can be prevented by having different operators for assignment and re-assignment. Some languages do that: in OCaml / F#, re-assignments use '<-' while declarations use 'let (mutable) x =' (they can't drop the 'let' because they use '=' as the Boolean equality operator).
Remember recently on the Java mailing list there was a lot of bikeshedding concerning this point. I don't think it matters near as much as the discussion surrounding it thinks.
Oh, and finally, "const" has a specific meaning: compile time constants.
Maybe other people's brains work differently than mine, but I'm sure I will be slightly slowed down by this.
I'm happy to be dependent on my IDE.
Only you can be sure of shortened words even if it looks obvious. Not good in a team.
I never understood people who shorten words to make the program look cryptic. Maybe it was someone who taught you how to program had it that way or somehow it makes you feel your program looks cooler if it looks more cryptic.
It might have appeared in earlier MLs as an extension.
I will echo what the others have said; it's not what I would choose, but in practice it's never been a problem when I'm writing Scala.
val array = new Array[String](10)
array.update(7, "hello")
array(7) = "hello" // simply calls out to updateAs a bit of an unreformed code golfer and a fan of ML-style syntax, I actually kinda like it myself; I was just listing a few things that are "considered questionable" (by some people) and present in both languages.
val = value (constant)
What can be mistaken?
l => let
v => var
You can argue that it's really not much extra work but it all adds up. Especially when you try to code on a tablet, which I find myself doing occasionally.