Object immutability

Not easy to guess; Java interop is (was ?) a boost for Kotlin and in same time a brake for some features ( immutable, reified type in non inline function, union type, type constructors …).
That says, it would be nice to have a “const” C++ equivalent in Kotlin

fun xx(const x : MyClass)  // xx can only call const functions of MyClass class for x.
{ ... }

fun other() : const MyClass { ... } // return an immutable instance of MyClass.
     
class MyClass() {
   const fun myFunc() : Int {  // myFunc is defined as const, does not modify this state.
   }
 }

This assure that x object will not be modified by xx function but not this object won’t be modified by another process (that’s more role of Lock classes).
This similar to ‘double interface’ system, but simpler.

1 Like