I have an interface with the following method in Java:
<E> Encoder1<E[]> arrayArg(Encoder1<? super E> elemEncoder);
I'm trying to implement it in Scala with the following declaration:
override def arrayArg[E](elemEncoder: Encoder1[_ >: E]): Encoder1[Array[E]]
I get the following error:
Error:(38, 16) overriding method arrayArg in trait Encoders of type [E](elemEncoder: redradishes.encoder.expr.Encoder1[_ >: E])redradishes.encoder.expr.Encoder1[Array[E with Object]];
method arrayArg has incompatible type
override def arrayArg[E](elemEncoder: Encoder1[_ >: E]): Encoder1[Array[E]] = ???
^
What is wrong with my Scala method signature?
Please see this: https://issues.scala-lang.org/browse/SI-4390
I'm not sure I fully understand why, but you need to specify E with Object
in return value for it to compile. The error informs you about that. Probably this is because in java you cannot use primitive types ( AnyVal
) as the generic parameter, but in scala you can so you need to specify that E
extends Object
to align it with Java rules. Still I'm not sure why this situation is so special.
If you are uncomfortable with Object
, E with AnyRef
also seem to work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.