How about a “generic nullable class”. My idea is t...
# language-evolution
b
How about a “generic nullable class”. My idea is that instead of nullable and not null, there is a third option “parameterized null” (
<?>
). Than you could do something like this
Copy code
data class Test<?>(
    val foo: Foo<?>,   // nullable depending on class parameter
    val bar: Bar,      // never null
    val baz: Baz:?     // allways nullable
)
val test1 = Test<?>(getFooOrNull(), Bar(), null)
val test2 = Test<!>(Foo(), Bar(), null)
test1.foo?.doSomething()
test2.foo.doSomething()
This could be used to easily create Builder classes for data classes with immutable values.
Maybe even allowing for multiple nullabllity parameters
class Foo<?SomeName, ?SomeOhterName>
k
class<?> is already means wildcard in java right? wouldn't it be confusing coming from java background? data classes could be improved with builders, I agree on that! but they are super easy to create with compiler plugins. I can not see any other options where this would be useful 🤔
k
How would you restrict that type? Say I wanted
data class Test
's generic parameter restricted to implementers of
CharSequence
b
What exactly do you mean? Do you want
Test
to extend
CharSequence
? This special generic parameter can only be
!
or
?
. If you want to mix this with a normal generic type you could do use
class Foo<?, T: CharSequence>
k
made it super clear 😄 what you are looking for is a Higher Kinded type.
class Foo<?, T: CharSequence>
is the same saying class `Foo<`Option<>`, CharSequence>` and
class Foo<!, T: CharSequence>
is the same saying class `Foo<`Id<>`, CharSequence>`
b
yes, exactly.
k
Okay, one more thing I can't quite understand. Today, all generic type parameters have names (usually
T
or something). However, here you just use
?
. How do you reference this without confusing it with the nullability symbol? Can your
Test
have multiple
?
parameters? If so, how do you distinguish which is which?
b
I did not spend much time thinking about a proper syntax. I just wanted to get the idea out there. I agree, just using
?
would probably not work. This could lead to multiple issues. I guess they will have to be named and than they could maybe all start with a
?
. Not sure yet.
1