Mark
11/12/2023, 4:33 AMAtomicReference
. But looking for a proper Kotlin way.ascii
11/12/2023, 4:44 AMlateinit
would be a perfect fit for the former (but that doesn't allow nullable types), otherwise there's no Kotlin sugar involved. You'll have to use something else to denote an uninitialized state, e.g. Object/Any.Mark
11/12/2023, 6:47 AMSam
11/12/2023, 7:10 AMLazy
delegate. For example:
val x: X? by lazy { /* init on first use */ }
Mark
11/12/2023, 7:11 AMSam
11/12/2023, 7:13 AMremember
instead of lazy
but I'm sure you've considered thatMark
11/12/2023, 7:15 AMFontFamily
)Mark
11/12/2023, 7:16 AMWeakHashMap
which doesn’t like null keys (which is what I would want for a null FontFamily
. I suppose I could use a custom non-null key insteadefemoney
11/12/2023, 12:33 PMprivate sealed interface Prop {
data object Uninitialized: Prop
data class Initialized(val value: Actual?): Prop
}
Stephan Schröder
11/12/2023, 2:08 PMprivate sealed interface Prop {
data object Uninitialized: Prop
@JvmInline value class Initialized(val value: Actual?): Prop
}
the @JvmInline
annotation is only necessary on a JVM backend.efemoney
11/12/2023, 2:36 PMDaniel Pitts
11/12/2023, 4:04 PMCLOVIS
11/12/2023, 4:30 PMsince this solution is only wrapping a single value, I'd use a value classNote that in this case it will always be boxed, because value classes are only inlined when used directly. If you declare the variable with an interface type, the class cannot be inlined.
ephemient
11/13/2023, 3:55 AMFor more background, I’m doing some text measurements.there are non-null "uninitialized" values you can use.
Dp.Unspecified
for exampleMark
11/13/2023, 5:24 AMephemient
11/13/2023, 5:39 AMDp
is density-independent, it's pixels that are density-dependent. in any case, perhaps modify the type to include some sentinel values? many other Compose types do similarMark
11/13/2023, 7:15 AMLocalDensity.current
value. Can be reused across densities etc. But yes, a single object
representing Unspecified
makes sense. There are no exposed properties, so it would just give dummy behavior.