Reputation: 2224
I want to make a function that takes an input type S
where S <: ParentClass
and S
also inherits SomeTrait
. I created a solution using S <: ParentClass with SomeTrait
and it compiles fine, however it rejects inputs that satisfy those conditions.
abstract class Units[T](v: T) { def getVal = v}
trait Dimension
trait Time extends Dimension
trait Quantity[T <: Dimension]
trait Instance[T <: Dimension] {
def plus[S <: Units[_] with Quantity[T]](q: S)
}
case class Seconds(v: Double) extends Units(v) with Quantity[Time] {
}
case class Timestamp(i: Int) extends Units(i) with Instance[Time] {
def plus[T <: Units[_] with Quantity[Time]](quantity: T) = Timestamp(2345/*placeholder value*/)
}
When I try to use this:
Timestamp(5).plus(Seconds(4))
I get the error:
<console>:46: error: inferred type arguments [Seconds] do not conform to method plus's type parameter bounds [T <: Units[_] with Quantity[Time]]
Timestamp(5).plus(Seconds(4))
^
<console>:46: error: type mismatch;
found : Seconds
required: T
Timestamp(5).plus(Seconds(4))
Bonus question: how do I get the value of an item with that type, as shown in the code?
Upvotes: 0
Views: 116
Reputation: 14073
Your code typechecks for me, both in Scala 2.11.8 and in Scala 2.10.6.
> console
[info] Starting scala interpreter...
[info]
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_31).
Type in expressions for evaluation. Or try :help.
scala> abstract class Units[T](v: T) { def getVal = v}
defined class Units
scala>
scala> trait Dimension
defined trait Dimension
scala> trait Time extends Dimension
defined trait Time
scala>
scala> trait Quantity[T <: Dimension]
defined trait Quantity
scala> trait Instance[T <: Dimension] {
| def plus[S <: Units[_] with Quantity[T]](q: S)
| }
defined trait Instance
scala>
scala> case class Seconds(v: Double) extends Units(v) with Quantity[Time] {
| }
defined class Seconds
scala> case class Timestamp(i: Int) extends Units(i) with Instance[Time] {
| def plus[T <: Units[_] with Quantity[Time]](quantity: T) = Timestamp(2345/*placeholder value*/)
| }
defined class Timestamp
scala> Timestamp(5).plus(Seconds(4))
(A 2.10.6 REPL console is basically identical, so I'll skip it.)
Upvotes: 1