https://kotlinlang.org logo
Title
b

Ben Piatt

11/19/2019, 9:21 PM
How do I pass a value of a non-null type to a method that accepts that type but as a nullable parameter? The other way around makes sense to safe-check, but if I'm giving you a non-nullable type as a parameter and you can accept nullable types, there's gotta be a way it can be taken
s

Shawn

11/19/2019, 9:23 PM
This should just work normally - you can definitely pass, for example
String
to a function that takes
String?
you should also be able to pass a non-null generic type to a function that takes a nullable one
Unless you mean you’re trying to pass a
Column<Int>
to a function that takes
Column<Int?>
b

Ben Piatt

11/19/2019, 9:26 PM
The latter
Sorry, not quite, Column<Int>?
s

Shawn

11/19/2019, 9:27 PM
If that’s the case, you should just be able to use it intuitively
b

Ben Piatt

11/19/2019, 9:27 PM
Actually so my full type is Column<EntityID<Int>?>
s

Shawn

11/19/2019, 9:27 PM
oh
b

Ben Piatt

11/19/2019, 9:27 PM
So the nullable part is the EntityID inside of the Column, that's what it doesn't like
k

karelpeeters

11/19/2019, 9:27 PM
Then you need to look at variance: https://kotlinlang.org/docs/reference/generics.html#variance
👆 1
b

Ben Piatt

11/19/2019, 9:27 PM
Ok thanks I'll take a look
k

karelpeeters

11/19/2019, 9:28 PM
Foo<X>
is not necessarily a subtype of
Foo<X?>
, it can be the other way around or not at all.
b

Ben Piatt

11/19/2019, 9:29 PM
Gotcha, ok yeah that kinda sucks because I have some helper methods that encapsulate common logic and I basically have to create a copy of the same method with a different name and it does the exact same thing just because of the type issue
s

Shawn

11/19/2019, 9:30 PM
you might be able to do what you want with the correct type projections, though
b

Ben Piatt

11/19/2019, 9:30 PM
I'll definitely try
k

karelpeeters

11/19/2019, 9:31 PM
Yeah it doesn't "suck", it's the typesystem forcing you to make your code more correct!
b

Ben Piatt

11/19/2019, 9:31 PM
Agreed haha I definitely like it better than the alternative