Bijon Desai
09/27/2021, 4:55 PMval isFord: Boolean
get() = carType == CarType.FORD
vs:
val isFord = carType == CarType.FORD
ephemient
09/27/2021, 5:02 PMget
, the latter is computed at assignment time and stored in a backing fieldBijon Desai
09/27/2021, 5:19 PMget()
as that makes the definition clearer.ephemient
09/27/2021, 5:22 PMephemient
09/27/2021, 5:22 PM==
Bijon Desai
09/27/2021, 5:24 PMLuke
09/27/2021, 5:28 PMinline get() = carType == CarType.FORD
, so that this method virtually does not existK Merle
09/28/2021, 3:39 PMinline get() = carType == CarType.FORD
and latter example? They are both computed once.ephemient
09/28/2021, 3:54 PMLuke
09/28/2021, 3:56 PMval isFord = carType == CarType.FORD
A variable is created in the class, is initialized to a boolean, and never changes afterward.
2️⃣
val isFord: Boolean
get() = carType == CarType.FORD
Basically. a method is created in the bytecode and called when the variable is used, and it returns the computation of carType == CarType.FORD
. The result will vary with carType
3️⃣
val isFord: Boolean
inline get() = carType == CarType.FORD
Whenever isFord
is called, the compiler replaces isFord
by the getter's body, in this case carType == CarType.FORD
, so there is no method/variable added to the class in the bytecodeephemient
09/28/2021, 4:01 PMLuke
09/28/2021, 5:10 PMK Merle
09/28/2021, 5:48 PM