6 ms·
Mutable vals in Kotlin
- epaga 9y agoThis is something Swift handles well - computed properties, even if they only have getters, are only allowed to be "var"s, not "let"s (Swift's equivalent to Kotlin's "val"). Looking over the "Learn Kotlin in X minutes" which was posted on HN a few days ago, I was amazed to see how similar Kotlin is to Swift - but this is one difference at least.
- trevor-e 9y agoMore importantly, Swift's structs come close to full immutability. If you declare a struct instance with `let`, the compiler throws an error if you try mutating its properties. You also have to mark functions in the struct as mutating if they modify any property. The only case this isn't true (and why I said comes close) is if you modify a property of a reference type within a struct.
- johnhenry 9y agoSimilar confusion in Java Script regarding const vs var.
- skrebbel 9y agoI like to overuse let for signalling mutable content. In other words, I only use const when the variable is read-only and never to be mutated. Even by that narrower ruleset, nearly all variables end up being const. This way a "let" signals to the reader "watch out, something is going to be mutated here", which IMO is a more useful note to the reader than "watch out, this reference can be changed".
- johnhenry 9y agoPerhaps I should have stated "const vs let", with "let" being JavaScript's modern, scoped version of "var"... but even so, I think this is just the sort of confusion that the author is trying to clear up. "let" in JS doesn't mean "mutable" just as "var" in Kotlin doesn't either. While one can use them as reminders to the programmer as to the mutability of the underlying object, there is nothing in the language that enforces this, so this is technically, syntactically incorrect. In both languages, it might make more sense to simply label variables directly with something like `${variable name}_IMMUTABLE`, which gives a clear clue to the programmer that the variable is immutable, but doesn't imply anything that's untrue about the how the language is used. Further, one can assign these variables using a library like immutable.js (https://facebook.github.io/immutable-js/ https://facebook.github.io/immutable-js/), or Kotlin's immutable collections (https://github.com/Kotlin/kotlinx.collections.immutable https://github.com/Kotlin/kotlinx.collections.immutable) to enforce a mechanism for mutability.
- blauditore 9y agoWhy not just Object.freeze()?
- johnhenry 9y agoFreezing an object doesn't freeze it's children, and thus it's still technically mutable. I think using deep-freeze (https://github.com/substack/deep-freeze https://github.com/substack/deep-freeze) might work.
- irishsultan 9y agoBecause that doesn't do what people want/expect, if you have a key with a value that's not frozen it will still be mutable, which indirectly mutates your object. See the example at https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze https://developer.mozilla.org/en/docs/Web/JavaScript/Referen... about modifying internal.a
- sjrd 9y agoNo, it isn't the same confusion. The article describes a different problem (worse, IMO). Read it again, or read the following comment in this HN thread: https://news.ycombinator.com/item?id=14445829 https://news.ycombinator.com/item?id=14445829
- johnhenry 9y agoAgreed, I don't believe that the confusions are the same, only similar.
- deleted 9y ago[deleted]
- deleted 9y ago[deleted]
- realharo 9y agoThey had a "feature poll" at a 1.1 event a while back, where one of the features you could vote for was "true immutability". Can't find any links now that explain how that would work though. I just remember that it was completely different from C++'s const-correctness (which is what I assumed at first when reading its title).
- jankotek 9y agoThis was discussed very very early. Basically type system would not allow to declare `var` inside class. It would apply to object graph, so immutable collection could not store mutable objects etc.. Problem is that type system in Java is dynamic at runtime. It is not possible while maintaining interoperability with Java code.
- lern_too_spel 9y agoKotlin is targeted at Java developers, for whom val is just a shorter way of saying final <type>.
- vbezhenar 9y agoval field is a shorter way of saying getField, actually, not final field, and that's the point of the article. Kotlin just doesn't have fields in Java sense (though it uses them, of course), for programmer they are properties, AKA getter+setter with trivial getter/setter generated automatically.
- jorgemf 9y agoThe article talks about immutable objects vs read-only objects. Final and Val means read only, the reference is immutable, not the object. For class variables Val and final are almost exactly the same.
- vbezhenar 9y agoval reference is not immutable. Your val can return new reference every time it's called: val x: String get() = (i++).toString() And JVM just don't have any notion about immutable objects, so Kotlin, obviously, don't have them either. You can return some ImmutableList and I'll change it using reflection and laugh at you. Immutability in Java is about conventions and trust.
- jorgemf 9y agoI said Val means read only, not immutable. I never said java have immutability (only for primitive values)
- swsieber 9y agoThough, you can add an annotation to get field behavior - it's mainly for java compatibility from what I can tell (and it's documented in the java interop section). I'm not sure if using that annotation also generates the getters/setters though.
- barrkel 9y agoTo do this the way Dan expects, you either need to have two parallel type systems (like C++'s const - a bad idea, because you ultimately end up needing to write const and non-const versions of methods) or you only allow expressions of immutable types to be made vals. Having immutable types as a first-class language concept is a good one, and much simpler to reason about, since it states an invariant that the compiler can check and that most programmers understand as a first-level concept.
- vbezhenar 9y agoCould you explain why would I need to write two versions of methods? I always liked C++ const-ness, the only drawback that I see is that const-ness should be default and something like `sideeffect` modifier should be applied for non-const functions.
- Ded7xSEoPKYNsDd 9y agoIn C++, there need to be e.g. two overloaded versions of the index operator on a vector: one taking a const vector reference returning a const reference to the value, and one taking a non-const vector reference returning a non-const reference to the value. With only the first one you couldn't modify values of a vector using nice [] syntax. With only the second one you couldn't use the same nice [] syntax on const vectors. So you need both. The implementation can be identical, but the method signature is different. You end up with such duplicate functions all the time for accessor methods.
- tekacs 9y agoThe author doesn't seem to be complaining about interior mutability, AFAICT. Scala makes the case the author is complaining about clearer than Kotlin, by using the 'def' keyword (same as functions/methods) for getters rather than 'val' (the latter can only be used for values). Matching the style of the article, it's: class Person(val birthDay: DateTime) { def age: Int = yearsBetween(birthDay, DateTime.now) } You can't use 'val' here and so excepting interior mutability, Scala 'val's don't change (and importantly, the reference you get by naming one doesn't ever change).
- 9y ago
- AzzieElbab 9y agoSame thing with scala or clojure. If the object is mutable setters and other methods can still mutate it
- lmm 9y agoIn Scala it's much less of an issue because the culture is immutable-first, and the library ecosystem is established enough that you don't often have to resort to a mutability-oriented Java library. I understand things are similar for Clojure.
- tormeh 9y agoI'm really annoyed by val as well, but the standard library is immutable-first so it's never really a problem for things you haven't written yourself. It can probably be a real pain for larger software with lots of invented-here classes, made by former Java devs, though.
- sjrd 9y agoNo that's true for Scala. Scala has, in fact three kinds of "properties": * `val foo: Int`: always immutable, really. Every access always returns the same value * `var foo: Int`: mutable * `def foo: Int`: "getter" of sorts; cannot be written to, but can return different values when called several times
- the_af 9y agoupvoted, but I think you meant "that's NOT true for Scala" (for the reasons you mentioned). Also, it should be clarified that `val` in Scala gets evaluated only once, which is why it cannot return different things at different times, unlike `def`.
- benjaminjackman 9y agoYou could also mention 'def foo_=(x: Int) : Unit' which is a setter. 'lazy val foo: Int' which is immutable but doesn't execute its right hand side until the first time it called and then stores and returns the value returned by the right hand side everytime. not that they are different enough to be mentioned
- opmac 9y agoSimple answer here is just don't define custom getters, however I do agree that such functionality shouldn't even be allowed by the language for `val` properties. Instead, if you want to define a custom getter, Kotlin should require that property to be a `var` (or `fun` since they're really functions).
- nostrademons 9y agoThat'd forbid a really common and innocuous use-case, where your custom val-getter is a simple function of other immutable data on the object. I use these all the time; they save on memory, allow lazy-init, and preclude the possibility of them getting out-of-sync with the source data if you later change their dependencies to mutable. (Although that introduces the issue described in the article; I would rather have code that is logically correct but surprising than code that is buggy.) A useful compromise might be for Kotlin to force 'var' if any of the fields used in the property body is itself a var, or is a function. It has this information already; it displays them in italics in the IDE, so no reason the compiler couldn't propagate that upwards.
- hota_mazi 9y ago> val does not mean immutable Yes it does. It means immutable reference.
- kodablah 9y agoThis becomes a big problem for smart casting, e.g. if (foo.bar is Baz). If truly immutable vals were allowed, bar could have its type assumed.
- gumballhead 9y agoThe person's birthday is immutable and can't change. The age isn't a mutable property -- it depends on what time we're comparing their birthday to. So it should be computed by a function as he himself says. The val not being immutable problem is more about declaring something like a list as a val (or final in Java), and being free to modify the elements within the collection anyway so long as the reference to the collection isn't reassigned. kotlin does have immutable versions of collections in its standard library. It's just not a language feature.