Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Kotlin's version is even better: val & var

Super clear. The "let" version smells a lot of "I have a CompSci PhD".



No, not at all. I hate var & val because I never can remember which is which (from Scala, which I never really used a lot).

let vs let mut is /clear/ - you can simply not mix them up.


How is it not clear that var is variable?


I'm a javascript developer.

if mutable is expensive, and should be difficult then "let mut" makes more sense.

var looks like a default to me, not a special case that needs care.


Nitpick: "expensive" may be the wrong word there, to me that implies some kind of runtime cost. The stated reason for the keyword is that it forces users to think a little bit more about mutability. You may want do this because safe mutability requires exclusive ownership.


Yes. I think the problem may be with the other one. "val" (and "let") don't seem obviously constant to me.


var => variable

val => value => is immutable

Of course people are different, but this seems clear as day, assmuning very basic familiarity with functional programming?


Yes, whenever I get confused I recall that var is obviously variable so therefore the other one must be val.


Let is definitely confusing given that JS has let and const (with let being mutable)


But that is cross-language, which is not ideal but not an internal problem if you work in one code base. (O)Caml also has `let` and is quite a bit older than JS.


C# uses “var” to mean “infer the type”, nothing to do with mutability or not.

“Let” feels like some in-joke, coming from a math via lisp heritage of “let k be any number...” to distinguish it in English from the surrounding writing. I have to guess that in early lisp (= k 5) would be an error as k isn’t defined and (k 5) errors because k isn’t a function and (let (k 5)) comes out of need for a binding function and why not “let”.

In c-like languages “k=5” is an established binding that both programmers and compilers can deal with. What does “let k=5;” add to Rust over “k=5;” ?


"val" is short for "value"


Practically, I prefer let and var because of autocompletion. Type one letter then tab. Of course, if I could dictate I might prefer something else.

I think of it as being more math like rather than PhD.

Nothing wrong with borrowing from math.


From the perspective of "language designers influencing programmers", the difficulty of auto-completing to `let mut` could be seen as something positive.


A good IDE will let you type "mu", and autocomplete with "let mu", cycling on tab will give you more options.


It's interesting if "let" nowadays has mostly 'academic' connotations.

30 years ago I think it would have reminded people more of BASIC or Fortran than (say) Scheme.


I think this would have been more true 10 years ago when Haskell and Lisp were the most likely place for people to have encountered "let".

Now? It's in Javascript in its ES6 evolution and related languages, Swift, Rust, and probably other "newer" languages I'm not aware of. Scala had it ten years ago also, but is more mainstream than it was then.


The hamming distance between val and var is tiny; easy to miss in a code review.


When assigning to val more than once: in a statically checked language the compiler will complain, and devs default to 'val' anyway. In a dynamic language you'd quickly get a runtime error.

Using 'var' instead of 'val' where possible: the compiler/linter will tell you this.

Or do you mean something else?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: