is there a way I can define a `NULL` as a companio...
# getting-started
e
is there a way I can define a
NULL
as a companion object of an inline class using a type parameter?
Copy code
@JvmInline
value class Ptr<T>(val adr: Adr = 0L) {
    companion object {
        val NULL: Ptr<*>
            get() = Ptr(MemoryUtil.NULL) // Not enough information to infer type variable T
    }
}
g
Ptr(MemoryUtil.NULL) -> Ptr<Nothing>(MemoryUtil.NULL)
or Ptr<*> to Ptr<Nothing>
I actually think that Null: Ptr<Nothing> is better, I don’t know how you are using T in this class, but probably there is no way to get anything from Null pointer, so Nothing is a good way to use Notning type here
e
I'm basically extending them with primitives and java class of the corresponding C structs, ie
Ptr<Byte>
or
Ptr<VkInstance>
your solution compiles fine, but if I try to use it, then I get an error
Copy code
val a: Ptr<Byte> = Ptr.NULL
Type mismatch.
Required: Ptr<Byte>
Found: Ptr<*>
I guess there is no way? I also tried to extend it on the
Ptr<Byte>.Companion
but it's not possible, no companion
e
You should declare the class as
Ptr<out T>
, then it’ll work with Nothing.
e
Thanks, it does work indeed Any contraindications with
out
?
g
Not really, looks that your case is a classic case of producer-object, official doc is pretty good in explanation of this: https://kotlinlang.org/docs/generics.html
👍 1