https://kotlinlang.org logo
Title
e

elect

08/26/2019, 12:57 PM
is there a possibility to use contracts for assuring the compiler a
val
interface field will be initialized exactly only once?
m

Marko Mitic

08/26/2019, 1:01 PM
What is your usecase here?
val
interface properties act as getter methods in java, I'm not aware of any way to tell compiler it will always return the same value
e

elect

08/26/2019, 1:03 PM
I know I'll init
names
exactly just once
interface Buf {
    companion object {
        val names: IntBuffer
    }
}
m

Marko Mitic

08/26/2019, 1:08 PM
yeah, compiler will force you to do so. However, interfaces don't have fields, only fieldless properties and actual field initialization is left to implementing classes
☝️ 1
what benefit would you have from being able to pass this info to compiler?
e

elect

08/26/2019, 1:10 PM
the same benefit you have using `val`s instead of `var`s: compiler optimizations?. Atm I'm using
laterinit var
though
m

Marko Mitic

08/26/2019, 1:11 PM
ok, one trick you can use here is overriding
val
property in interface with
var
property in class
if that's helpful
e

elect

08/26/2019, 1:12 PM
that interface is supposed to be used in an enum though
m

Marko Mitic

08/26/2019, 1:18 PM
I'm not exactly sure what your issue here is, could you share some code snippet?
e

elect

08/26/2019, 1:19 PM
to be honest, I'm experiment, my goal is actually to have an enumerator class backed by and
IntBuffer