4 ms·
Kotlin is targeted at Java developers, for whom val is just a shorter way of saying final <type>.
by lern_too_spel 9y ago
Kotlin 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.
- opmac 9y agoNot sure if you read the post or not because it's really complaining about the fact that you can define custom getters for `val` fields in Kotlin. There is no real equivalent in Java here since Kotlin overloads class fields such that they represent both functions and variables (they're similar to C# properties).
- deleted 9y ago[deleted]