https://kotlinlang.org logo
#language-evolution
Title
# language-evolution
b

Burkhard

11/24/2019, 9:36 AM
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

kioba

11/25/2019, 11:45 AM
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

Ky Leggiero

11/26/2019, 11:46 PM
How would you restrict that type? Say I wanted
data class Test
's generic parameter restricted to implementers of
CharSequence
b

Burkhard

11/27/2019, 3:49 PM
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

kioba

11/27/2019, 3:55 PM
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

Burkhard

11/27/2019, 3:56 PM
yes, exactly.
k

Ky Leggiero

11/28/2019, 12:06 AM
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

Burkhard

11/28/2019, 1:47 AM
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
3 Views