https://kotlinlang.org logo
#language-proposals
Title
# language-proposals
a

Ayla

03/24/2024, 7:10 AM
I don’t know if similar topics have been discussed:
const
class and
const
collection
const
class restricts all field types to be immutable (non-read-only), which can bring more optimization possibilities for hashcode (caching, just like string) and copy (reusing immutable fields instead of full copy), and this helps ensure multi-thread safety.
Copy code
const data class A(
  val int:Int,
  var mutableField:String,//error: fields of a const class should be immutable
  val list:List<String>
)
const val instance= A(1,"",const ["1","2","3"])
const
collection can ensure that the collection is immutable, which can be combined with collection literals to eliminate the copy overhead of varargs
Copy code
fun simpleFunction(const list:List<String>){}
fun varargsFunction(vararg item:String){}

var str=""
const val c1=const ["",str]//error:The elements of a const collection should be immutable
const val c2=const [""]

simpleFunction(list2)
varargsFunction(c2)