Mutable vals in Kotlin
blog.danlew.net
blog.danlew.net
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.
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).
Problem is that type system in Java is dynamic at runtime. It is not possible while maintaining interoperability with Java code.
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.
* `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
val foo: Object
will always return the same reference, but the state of the referenced object may have been mutated, which isn't what I'd expect when you say "always immutable, really".
In Scala it cannot happen that you call a `val` twice and it returns a different object.
'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
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`.
Yes it does. It means immutable reference.
I'm not sure if using that annotation also generates the getters/setters though.
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.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.
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).Swift also classifies methods between regular (accessible on both var and let binding) and `mutating` (only accessible on let bindings), though I think that also works for value types (structs).
[0] https://doc.rust-lang.org/book/mutability.html#interior-vs-e...
Just to clarify the terminology, the `mutating` keyword in Swift is only used with value types (`struct` and `enum`) and is only accessible on `var` bindings (`let` bindings are readonly).
IMO allowing interior mutability by default on `let`-bound reference (`class`) types was a mistake in the language design and the same syntax should've been required there, but I assume Apple wanted to make things clearer for programmers who don't have a C/C++/similar background and so don't yet understand the distinction between [stack-allocated, pass-by-copy] values and references [to heap-allocated, reference-counted objects].
In C++ terms:
struct O {int f;};
template<class T> struct SwiftRef : shared_ptr<T> {
template<class U> SwiftRef(U u) : shared_ptr<T>(new T{u}) {}
operator T() const noexcept {return **this;}
};
O struct_var_binding{4};
const O struct_let_binding{4};
SwiftRef<O> class_var_binding{4};
const SwiftRef<O> class_let_binding{4};
// What it should be, IMO: const SwiftRef<const O> class_let_binding{4};They also likely didn't want to make the work of bridging/using Obj-C types (which I believe are all reference types) even harder.
Should read "var bindings" (mutable bindings), sadly the edition window is now closed.
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.
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.
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".
"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/), or Kotlin's immutable collections (https://github.com/Kotlin/kotlinx.collections.immutable) to enforce a mechanism for mutability.
See the example at https://developer.mozilla.org/en/docs/Web/JavaScript/Referen... about modifying internal.a