0

Given a few traits:

scala> sealed trait Foo
defined trait Foo

scala> sealed trait Parent
defined trait Parent

scala> case class Kid(name: String) extends Parent
defined class Kid

scala> sealed trait HasNickname
defined trait HasNickname

I attempted to define a method that takes an A that satisfies:

  • sub-class of Parent
  • implements trait HasNickname
  • implements trait Foo

But I got a compile-time error:

scala> def f[A <: Parent : Foo with HasNickname](x: A): A = ???
<console>:14: error: Foo with HasNickname does not take type parameters
       def f[A <: Parent : Foo with HasNickname](x: A): A = ???
                         ^

How can I modify f to fit the above criteria?

Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
  • 1
    Actually, I think that `def f[A <: Parent with Foo with HasNickname](x: A): A = ???` will work. – Kevin Meredith Nov 28 '15 at 19:35
  • Indeed. The single `:` in the type parameters is used for [context bounds](http://stackoverflow.com/questions/2982276/what-is-a-context-bound-in-scala). – Jesper Nov 28 '15 at 20:09

0 Answers0