Kotlin has a const-keyword. But I don't think constants in kotlin are what I think they are. It seems to very different to const in C++. It seems to me that its is only available for static members and to what are primitives in Java and do not compile for class-variables:
data class User(val name: String, val id: Int)
fun getUser(): User { return User("Alex", 1) }
fun main(args: Array<String>) {
const val user = getUser() // does not compile
println("name = ${user.name}, id = ${user.id}")
// or
const val (name, id) = getUser() // does not compile either
println("name = $name, id = $id")
}
As this seems not to work, I think what i really want is a second class, that deletes the operations i don't want to support:
class ConstUser : User
{
ConstUser(var name: String, val id: int) : base(name, id)
{ }
/// Somehow delte the setters here?
}
The obvious downside to such a apprach is that I must not forget to change this class, in case i change User
, something that looks very dangerous to me.
But I'm not sure how to do this. So the question is: How does one make immutable objects in ideomatic Kotlin?
const
is not about immutability. It's a signal for compiler to replace usages of such property with raw value during compile time. Immutability is achieved by not exposing mutating accessors. CompareCollection
andMutableCollection
interfaces in Kotlin stdlib – Casabonne