Home Ask Login Register

Developers Planet

Your answer is one click away!

user3324500 February 2016

Is it possible to "mix in: additional type constraints

I'd like to add constraints to a type field in a trait as additional traits are mixed in but can't seem to find a way to express it. Any help appreciated:

trait Events { def be() = println("B") }
trait Create { self : Events => def create() = println("C") }
trait Update { self : Events => def update() = println("U") }
trait Delete { self : Events => def delete() = println("D") }

trait EventHandler {
  type MyEvents <: Events
  val myEvents : MyEvents

trait Creation {
  self: EventHandler =>
  override type MyEvents <: Create

def h(events: EventHandler with Creation) : Unit = {
  events.myEvents.be()  // <== wont' compile

Compiler says:

<console>:12: error: value be is not a member of events.MyEvents


LaloInDublin February 2016

Ok some of my previous comments were in error, I didn't realize self typing hides the interface you're working with and thus is not the same as "extends". Here's a link that explains it better:


There's too many logical conflicts this creates in your code, and so I won't attempt to rework it here. Those conflicts in your code come down to this - some of your logic is opening up the trait API, and some of your self-types are then immediately hiding them. Here's an example that helped me understand why

trait A {
   def x:Int
  trait B { self:A =>


  def workWithB(b:B): Unit ={
    b.x  //wont' compile, is forced hidden.

Post Status

Asked in February 2016
Viewed 3,524 times
Voted 8
Answered 1 times


Leave an answer

Quote of the day: live life