bbaldino
07/24/2020, 5:35 PMT
bounded by Any
and call a method which returns a T
, but in this case T
is a java type (an enum) and the method (defined in java) returns null. I expected an exception to be thrown here, but instead the value of null trickles up just fine, and a (kotlin) method which is set to return a value T : Any
ends up happily returning null. Is it expected that this won't throw somewhere? Is this just a limitation of interacting with Java types from kotlin?streetsofboston
07/24/2020, 5:48 PMMyEnum!
, with the exclamation mark !
).
Values of this type can be null but can be assigned to non-nullable variables.
At some point, when you finally access the platform-type value and expect it to be not null, you’ll get a null-pointer-exception.bbaldino
07/24/2020, 5:51 PMstreetsofboston
07/24/2020, 6:16 PMreturn
statement as well… (get(): NewType =
), but I guess it doesn’t….bbaldino
07/24/2020, 6:16 PMConverter#get
to:
fun get(): NewType = converter(originalValue).also {
if (it == null) {
println("null!")
}
}
then kotlin warns me the check is redundant (since it thinks it
can't be null), but it does print null!
... 😛val a = Converter<String, MyEnum>("blah") { MyEnum.fromString(it) }
println(a.get())
but this will print `null`:
val c = Converter<String, MyEnum>("blah", MyEnum::fromString)
println(c.get())