简体   繁体   中英

Scala Loses Generic Type of Parameter

In over my head dealing with a tricky covariant type used in an inherited trait's overridden function. The basic question is, what is the [?] type? I can't find a good definition (it's kinda ungooglable), and so it's unclear why a [T] gets replaced with [?] in the following example:

> sealed trait Bar[+T]
> trait FooT { type Other; def foo[T,V](stuff:Bar[T]*) { stuff.headOption.isDefined } }
> trait TooF extends FooT { override def foo[T,V](stuff:Bar[T]*) { super.foo(stuff) } }
<console>:7: error: type mismatch;
found   : Bar[T]*
required: Bar[?]
   trait TooF extends FooT { override def foo[T,V](stuff:Bar[T]*) { super.foo(stuff) } }

I'm not sure of the exact reason that it shows Bar[?] but I think it probably something like the type parameter hasn't been resolved yet. The real problem is that your syntax for passing the varargs on to the super method is incorrect. It should be

override def foo[T,V](stuff:Bar[T]*) { super.foo(stuff:_*) }

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM