Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin read only property with and without getter

Tags:

kotlin

Are these equivalent?

  • val foo = someFooReturningFunction()

  • val foo get() = someFooReturningFunction()

The way I understood the documentation they were, but in my own testing they are not.

With the get() someFooReturningFunction() is evaluated each time the property is accessed, without it is only evaluated once.

like image 305
chrisortman Avatar asked Feb 09 '17 18:02

chrisortman


People also ask

Are getters and setters necessary in Kotlin?

In programming, getters are used for getting value of the property. Similarly, setters are used for setting value of the property. In Kotlin, getters and setters are optional and are auto-generated if you do not create them in your program.

What is lateInit and lazy in Kotlin?

Lazy initialization is one of the property Delegate-s, while late initialization requires the use of a language keyword. Lazy initialization applies only to val, and late initialization applies only to var fields. We can have a lazy field of a primitive type, but lateinit applies only to reference types.

What are Kotlin's properties?

Properties are the variables (to be more precise, member variables) that are declared inside a class but outside the method. Kotlin properties can be declared either as mutable using the “var” keyword or as immutable using the “val” keyword. By default, all properties and functions in Kotlin are public.

What is lateInit VAR in Kotlin?

The lateinit keyword allows you to avoid initializing a property when an object is constructed. If your property is referenced before being initialized, Kotlin throws an UninitializedPropertyAccessException , so be sure to initialize your property as soon as possible.


2 Answers

They are not equivalent. The custom getter is indeed evaluated on each property access, similarly to a normal function, while a val property with no custom accessors is only evaluated once on initialization (and is actually stored in a final field on JVM platform).

Here are at least a few more differences:

  • The control flow analysis and nullability inference takes it into account if a property has a custom getter (or is open and thus might be overridden with a custom getter), because there's no guarantee that the property returns the same value on successive calls:

    if (someObject.defaultGetterProperty != null) {
        someObject.defaultGetterProperty.let { println(it) } // OK
    }
    

    if (someObject.propertyWithCustomGetter != null) {
        someObject.propertyWithCustomGetter { println(it) } // Error: cannot smart-cast
    }
    
  • When a property is private, if it has no custom getter then the getter is not generated at all and the backing field is accessed directly. This, however, is an implementation detail and not something to rely on.

like image 80
hotkey Avatar answered Oct 21 '22 16:10

hotkey


No. In addition to @hotkey's reasons, here's a simple demonstration using mutable properties showing when they're definitely not equivalent. TLDR: if your property is calculated using a mutable property, always use a custom getter over an initializer.

data class Calculation(val value1: Int, var value2: Int) {
    val sum: Int = value1 + value2
    val sumWithGetter: Int
        get() = value1 + value2
}

val calculation = Calculation(1, 2)
println(calculation.sumWithGetter) // prints 3
println(calculation.sum)           // prints 3

calculation.value2 = 0
println(calculation.sumWithGetter) // prints 1 (correct)
println(calculation.sum)           // prints 3!
like image 35
James Bassett Avatar answered Oct 21 '22 16:10

James Bassett