Hey guys. Is there any possibility to provide cast...
# announcements
a
Hey guys. Is there any possibility to provide casting for such a Java class in Kotlin:
public abstract class NumberExpression<T extends Number & Comparable<?>> extends ...
I've tried to do this
(expression as NumberExpression).goe(value)
, but this does not work. I know, this is not a good practice, just to know... 🙂
d
What type is
value
and whats the parameter type for
goe
?
a
Type of
value
and parameter type of
goe
is
T
from declaration of NumverExpression class.
d
Then there should not be the need for any cast? If you have
val value: T
then you can just pass it in.
a
Ou, I thought you mean the type in scope of NumberExpression. Type of
value
is different from
T
in my class. That's why I need casting...
The problem here is that the
NumberExpression
class uses generics for
Number
and
Comparable<*>
at the same time.
d
This is the best I could come up with, using two unchecked casts. You need a helper function so you can introduce an additional generic parameter.