7 ms·
> Declaring values is done using either "var" or "val". "val" declarations cannot be reassigned, whereas "vars" can. I wonder why they decided on these very mi
by coldpizza 9y ago
> Declaring values is done using either "var" or "val". "val" declarations cannot be reassigned, whereas "vars" can.
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?
- bschwindHN 9y agoMy guess is it came from Scala? In practice it's not very tough to get it right.
- throwaway7645 9y agoVal might have originally been OCaml or Haskell.
- mahmud 9y agoStandard ML, rather. It might have appeared in earlier MLs as an extension.
- throwaway7645 9y agoAnd I was soooo close :)
- bschwindHN 9y agoI mean the combination of val (immutable) and var (mutable) as keywords to declare bindings. OCaml uses let and so does Haskell, right?
- vnglst 9y agoI'm guessing here: With const there's always the disccusion about consts not being constant, e.g. when referencing an array or object (reference itself is constant). With "val" you don't have that confusion?
- mattnewton 9y agoBut why not let? Or any word that doesn't start with "va"?
- meddlepal 9y agoI've been writing Kotlin for a year and a half and it has yet to be an issue.
- wavefunction 9y agoWith an IDE you could apply different highlighting styles to the two to minimize confusion.
- namelezz 9y agovar = variable (can vary/change) val = value (constant) What can be mistaken?
- kibwen 9y agoPet peeve here: "variable" and "mutable" aren't concepts that are useful to conflate. Imagine a function that takes a variable and prints its value to the command line. There's no mutation of the variable going on, so the variable is immutable. And yet it's still a variable because each time that you call this function, the value that gets printed is allowed to vary. This is distinct from a constant, where each time you call the function the value of a constant must be the same.
- thatswrong0 9y agoAt a quick glance one might mistake var for val since they're only one letter apart. The definitions do make sense though.
- melling 9y agoI don't find it to be confusing but you'd get more autocomplete help if let was used for constants. l => let v => var
- dajohnson89 9y agoVery good point. Fortunately, it's only one keystroke saved.
- NinoScript 9y agoIt could be two if it inserted the space for you!
- melling 9y agoOne keystroke, not two? I type l<tab> in Xcode then I'm typing in the variable name. You'll have to arrow down if it's the wrong one then hit return. 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.
- kodablah 9y agoKotlin as a language has expectations on IDE capabilities. I doubt the problem of accidentally typing "r" instead of "l" at the end of a keyword is as prevalent as someone typing "var" instead of "const" and not coming back to make it immutable. A quality compiler can warn about an unmutated mutable variable which solves all problems. 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.
- BurningFrog 9y agoThe problem isn't typing, but reading. Maybe other people's brains work differently than mine, but I'm sure I will be slightly slowed down by this.
- Larrikin 9y agoWhen it comes to reading it is very clear in the IDE. val variables look normal (since immutability is encourage), var variables are underlined. You get used to it very quickly
- BurningFrog 9y agoThat does help a lot. I'm happy to be dependent on my IDE.
- flamedoge 9y agomy beef is with 'val' being keyword. How many variables did you name val in all your coding? I certainly did.
- j_jochem 9y ago`val` is never an appropriate name for a variable because it tells you nothing at all about what the variable contains.
- sushisource 9y agoProbably because that's what scala does
- saghm 9y agoI'm personally a fan of the Rust way, i.e. `let` for const bindings and `let mut` for mutable ones. I think it's fairly clear which is which, and having to type four extra characters to get mutability helps reinforce the notion of const being the default choice.
- dcow 9y agoSure, but Kotlin is more about being explicit and less about evangelizing one style of programming over another. It's very pragmatic in that way. There are plenty of scenarios where you're maintaining local state (properly encapsulated within a class, exposing a functional interface, etc) and you need mutable fields. Even plenty of scenarios where you want to operate on a value mutably in a function and then return it. It may seem confusing at first but in practice I've never mixed the two up. But this most likely is because Scala uses val/var.
- Jach 9y agoIf your 'let' doesn't propagate so that immutable collections are used, it's not very valuable. Just like 'final' in Java doesn't prevent anyone from mutating your ArrayList. Using 'val' instead of something more suggestive like final/const/'not mut' seems a lot nicer to me. (Edit: and indeed Kotlin having "mutableListOf" and "listOf" separation is a good step in readability. The val/var before either of those is less important.)
- always_good 9y agoThat's a misnomer, not a misgiving. 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.
- idbehold 9y agoEven with Object.freeze() it only prevents mutation of the top-level keys.
- gizmogwai 9y agoConst exists too, but for compile-time constant. Var and val are ok due to the general conciseness of the language (they don't get lost in the noise of other ceremonies). Plus, being a language though to benefit from strong tooling, IDE's will generally display the two differently.
- bxrxaxdx 9y agoIn my experience as someone whose been writing scala for a couple of years (which also uses val/var), it has 100% never been confusing or a problem.
- Cyph0n 9y agoI second this. Not to mention that you end up using val most of the time in practice, so there really is no confusion.
- whateveracct 9y agoscala-mode2 (which isn't close to an IDE) also highlights vars in red so it's extra hard to get mixed up :)
- opportune 9y agoYep. I think the names themselves are quite unambiguous to boot. A variable is something that varies in value, meaning it can be changed. A value is always that same value; 5 is never 6. Pretty straightforward if you ask me.
- wapz 9y agoI'm guessing the parent is just complaining that if you are reading code very fast (particularly if you're browsing repositories on your phone) it is hard to distinguish between "val" and "var."
- h1d 9y agoLet alone type them wrong on a tired night.
- thomastjeffery 9y agoThankfully that would be a simple compile-time error. The message might even be readable.
- smitherfield 9y agoIt's obviously very strongly influenced by Scala, to the point of adopting a number of its syntax decisions that are considered questionable (procedure syntax, infix notation, "=" function definitions). 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.
- Cyph0n 9y agoWhy are Scala's "=" function definitions questionable? It makes absolute sense since a function should return a value.
- joshlemer 9y agoThey might be referring to the update methods: val array = new Array[String](10) array.update(7, "hello") array(7) = "hello" // simply calls out to update
- smitherfield 9y agoFor the same reason omitting braces in C-style-syntax conditionals and loops is questionable, i.e. misleading indentation. As 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.
- hota_mazi 9y agoThis is the usual first thought of someone who's never used the language. A few hours after writing some Kotlin code, you don't even think about it.
- nonsince 9y agoRust has a pattern-matching "let" and then you can prefix names within any pattern with "mut" to make it mutable. So a basic mutable var is "let mut" but it's just a combination of two other features. I think it's a totally neat design.
- dewiz 9y agoI find this syntax very clear, usually start with val and try to stick to it. Moving to var is one char-change only. Also val stands for "value" which is a well known term for immutable data. I think about const as static values, while val are instance values.
- jackmott 9y agovalue as being immutable is a common idiom
- FLUX-YOU 9y agoI bet you can change the val color to something that contrasts the var color in an IDE.
- mike_hearn 9y agoThe IDE underlines mutable variables at both definition and use sites. Values are not underlined. It's a perfectly fine way to visually distinguish them.