Matheus
10/28/2019, 8:31 PMClass<T>
return type, but I can't seem to make it work with nullable types without triggering a compiler warning
override fun toType(): Class<Boolean?> =
Boolean::class.java as Class<Boolean?>
This works, but the cast triggers the warning: Unchecked cast Class<Boolean> to Class<Boolean?>
I tried to qualify it as <out Boolean>, but I can't seem to make it work?Zachary Grafton
10/28/2019, 8:52 PMClass<Boolean>?
instead?Matheus
10/28/2019, 8:54 PMClass<T>
override fun toType(): Class<Boolean?> =
toTypeReified()
private inline fun <reified T>toTypeReified(): Class<T> = T::class.java
but I'm not sure it's the best solutionRuckus
10/28/2019, 8:55 PMClass<Boolean?>
, so you may need to look at your generics.Zachary Grafton
10/28/2019, 9:03 PMClass<Boolean?>
What is the actual return type of the method from the Java interface? Assuming that Java interface isn't marked with @Nullable
the return type should be Class<Boolean>!
So, in this case, Class<Boolean>
or Class<Boolean>?
should work.Matheus
10/28/2019, 9:08 PMBoolean
before, since I always use it with Boolean? types :grinning_face_with_one_large_and_one_small_eye:Class<Boolean?>
doesn't exist , I didn't get any compiler errors?Zachary Grafton
10/28/2019, 9:11 PMClass<Boolean?>
implies that Class<Boolean>
and Class<null>
are valid. Class<null>
isn't possible. Class<Boolean>?
implies that null is a valid return value.Class<null>
doesn't work because null
is a primitive type and you can't pass primitive types as type parameters to any generic.Matheus
10/28/2019, 9:24 PMimplies that [...] Class<null> is valid
bit.
Isn't the T in generics supposed to be the "placeholder of a type"? And isn't Boolean?
a type?Zachary Grafton
10/28/2019, 9:27 PMBoolean?
is a type, but there are restrictions on what you can pass to generics as parameters.Class
is even more specialMatheus
10/28/2019, 9:30 PMZachary Grafton
10/28/2019, 9:31 PMMatheus
10/28/2019, 9:31 PMdiesieben07
10/29/2019, 8:10 AMBoolean
, Kotlin cannot know if it's nullable or not, so it sees it as a platform type, which adjusts to being nullable or not nullable depending on context.
If you are overriding a method, IntelliJ will choose the conservative way and choose nullable. but in this case that's not correct, so if you change the return type to Class<Boolean>
your code still compiles and makes sense.Matheus
10/29/2019, 12:00 PMClass<T> toType();
It is applied to my Kotlin class, tho, which has a Boolean?
typeclass EnabledIndicatorConverter: Converter<BigDecimal, Boolean?> {
override fun to(isEnabled: Boolean?): BigDecimal? {
return isEnabled?.let { BigDecimal.ONE }
?: BigDecimal.ZERO
}
override fun fromType(): Class<BigDecimal> =
BigDecimal::class.java
override fun toType(): Class<Boolean?> =
Boolean::class.java as Class<Boolean?>
}
diesieben07
10/29/2019, 1:07 PMclass EnabledIndicatorConverter: Converter<BigDecimal, Boolean> {
override fun to(isEnabled: Boolean?): BigDecimal? {
return isEnabled?.let { BigDecimal.ONE }
?: BigDecimal.ZERO
}
override fun fromType(): Class<BigDecimal> =
BigDecimal::class.java
override fun toType(): Class<Boolean> =
Boolean::class.java
}
Matheus
10/29/2019, 2:23 PM