Yes, though I guess it depends on what you need to express.
T?
is meant to express "the nullable version of T, no matter whether T is already nullable or not". Note that an unconstrained T is already potentially nullable, depending on the type parameter that's actually passed in a specific usage of your generic declaration
k
Kevin Del Castillo
11/07/2022, 2:11 PM
As Joffrey Bion mention you'd have to constraint your type
T
to
T: Any
then you should be able to use
T?
as an "optional"
T
y
y
11/07/2022, 2:11 PM
wait, is this true for a concrete
T
?
y
11/07/2022, 2:12 PM
as in,
val foo: Foo?
is nullable, but
val foo: Foo
is not, correct?
๐ 2
k
Kevin Del Castillo
11/07/2022, 2:12 PM
No for concrete types it's not same, the implicit nullability of the type is only true for generics
y
y
11/07/2022, 2:13 PM
okay, phew.
j
Joffrey
11/07/2022, 2:13 PM
Foo
is not nullable,
Foo?
is nullable. Both can be passed as
T
to a generic declaration if
T
is not constrained
๐ 2
y
y
11/07/2022, 2:15 PM
oh, yes. that also aligns with my understanding. thanks about that clarification.
so, just making sure: say Iโm making a Kotlin class, and I need an optional field (of a non-generic type, letโs say