score:51

Accepted answer

with means that the class is using a Trait via mixin.

Post has the Trait IdPK (similar to a Java class can implements an Interface).

See also A Tour of Scala: Mixin Class Composition

score:21

While this isn't a direct answer to the original question, it may be useful for future readers. From Wikipedia:

Scala allows to mix in a trait (creating an anonymous type) when creating a new instance of a class.

This means that with is usable outside of the top line of a class definition. Example:

trait Swim {
  def swim = println("Swimming!")
}

class Person

val p1 = new Person  // A Person who can't swim
val p2 = new Person with Swim  // A Person who can swim

p2 here has the method swim available to it, while p1 does not. The real type of p2 is an "anonymous" one, namely Person with Swim. In fact, with syntax can be used in any type signature:

def swimThemAll(ps: Seq[Person with Swim]): Unit = {
  ps.foreach(_.swim)
}

EDIT (2016 Oct 12): We've discovered a quirk. The following won't compile:

 // each `x` has no swim method
 def swim(xs: Seq[_ >: Person with Swim]): Unit = {
   xs.foreach(_.swim)
 }

Meaning that in terms of lexical precedence, with binds eagerly. It's _ >: (Person with Swim), not (_ >: Person) with Swim.


Related Query

More Query from same tag