If I have: ```@JvmInline value class Incomplete<T>...
# getting-started
d
If I have:
Copy code
@JvmInline
value class IncompleteT(private val value: T) {
....
    // this won't compile
    fun filterNotNull(): IncompleteT & 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?)?
Copy code
@Suppress("UNCHECKED_CAST")
    fun R : T & Any filterNotNull(): IncompleteR? =
        if (value != null) this as IncompleteR else null
(this forces me to specify the type at the call site...)
e
it works without any extra type parameter
R
as an extension
Copy code
class Incomplete<out T>(private val value: T)
fun <T: Any> Incomplete<T?>.filterNotNull(): Incomplete<T>?
which is what stdlib does
d
Then I don't have access to the private value...
Copy code
fun T: Any IncompleteT?.filterNotNull(): IncompleteT? =
    if (value != null) this as IncompleteT else null
imports
Copy code
import sun.jvm.hotspot.oops.CellTypeState.value
instead of using the value class's value @ephemient
y
You could write it in the companion object which will give you access to the
private
one I believe
👍🏼 1
d
Thanks! And Intellij seems to import that automatically...
e
or use
@PublishedApi internal
d
Yeah, I'd have to maybe separate this into a different gradle module or library for that... but it might be better in the end. But why do I need the PublishedApi annotation?
e
Copy code
@JvmInline
value class Incompleteout T(@PublishedApi private val value: T)
allows
Copy code
inline fun T: Any IncompleteT?.filterNotNull(): IncompleteT? = if (value != null) Incomplete(value) else null
to access
internal value
within the same module
d
You mean
Copy code
@JvmInline
value class Incomplete<out T>(@PublishedApi >>>internal<<< val value: T)
Or does it work for
private
too?
Also, I thought
internal
by itself already allows that access?
e
ah I meant
internal
internal
by itself allows that access for non-
inline
functions,
@PublishedApi
is necessary for
inline
functions because they get inlined into other modules
👌🏼 1