Scala generic multiple type variant

43 Views Asked by At

i do some experiments with Scala types

and tried to do something like that:

case class Foo(name: String)
defined class Foo

scala> case class Bar(id: Int)
defined class Bar

scala> def process[A <: Foo with Bar](x: A) = println(x)
process: [T <: Foo with Bar](x: T)Unit

tried to run this:

scala> process[Foo](Foo("test"))
<console>:15: error: type arguments [Foo] do not conform to method process's type parameter bounds [T <: Foo with Bar]
           process[Foo](Foo("test"))

scala> process[Bar](Bar(1))
<console>:15: error: type arguments [Bar] do not conform to method process's type parameter bounds [T <: Foo with Bar]
       process[Bar](Bar(1))

how do i can do this correctly without trait addition?

trait Printable
class Foo extends Printable
class Bar extends Printable

def process(x: Printable) = println(x)

Not like this. Is there are some other ways?

0

There are 0 best solutions below