dave08
10/31/2024, 3:40 PM@JvmInline
value class Incomplete<T>(private val value: T) {
....
// this won't compile
fun filterNotNull(): Incomplete<T & Any>? =
if (value != null) this else null
...
}
I don't want T on the class to inherit Any, since it can be null but I want the instance returned to not be nullable... could this solve the problem (is there a better way?)?
@Suppress("UNCHECKED_CAST")
fun <R : T & Any> filterNotNull(): Incomplete<R>? =
if (value != null) this as Incomplete<R> else null
(this forces me to specify the type at the call site...)ephemient
10/31/2024, 3:48 PMR
as an extension
class Incomplete<out T>(private val value: T)
fun <T: Any> Incomplete<T?>.filterNotNull(): Incomplete<T>?
which is what stdlib doesdave08
10/31/2024, 3:51 PMdave08
10/31/2024, 3:51 PMfun <T: Any> Incomplete<T?>.filterNotNull(): Incomplete<T>? =
if (value != null) this as Incomplete<T> else null
imports
import sun.jvm.hotspot.oops.CellTypeState.value
instead of using the value class's value @ephemientYoussef Shoaib [MOD]
10/31/2024, 3:57 PMprivate
one I believedave08
10/31/2024, 4:00 PMephemient
10/31/2024, 4:00 PM@PublishedApi internal
dave08
10/31/2024, 4:02 PMephemient
10/31/2024, 4:06 PM@JvmInline
value class Incomplete<out T>(@PublishedApi private val value: T)
allows
inline fun <T: Any> Incomplete<T?>.filterNotNull(): Incomplete<T>? = if (value != null) Incomplete(value) else null
to access internal value
within the same moduledave08
10/31/2024, 4:07 PM@JvmInline
value class Incomplete<out T>(@PublishedApi >>>internal<<< val value: T)
dave08
10/31/2024, 4:07 PMprivate
too?dave08
10/31/2024, 4:08 PMinternal
by itself already allows that access?ephemient
10/31/2024, 4:40 PMinternal
ephemient
10/31/2024, 4:41 PMinternal
by itself allows that access for non-inline
functions, @PublishedApi
is necessary for inline
functions because they get inlined into other modules