UPDATED: simplified code that runs in repl
I want to create domain-specific events like Created, Updated, Deleted, etc using path-dependent types that extends a common marker trait so that domains can (a) send each other events and (b) pattern match by domain:
trait Event
trait Domain {
case class Created(name: String) extends Event
}
// declare three instances of Domain trait, one here and two
// in an inner scope
object DomainC extends Domain
{
object DomainA extends Domain
object DomainB extends Domain
def lookingForAs(event: Event): Unit = {
println(s"${event.getClass}")
event match {
case DomainB.Created(_) => println("Oops matched a B")
case DomainC.Created(_) => println("Oops matched a C")
case DomainA.Created(_) => println("Found my A")
}
}
lookingForAs(DomainA.Created("I am an A"))
}
The result is "Oops matched a C"
The lookingForAs
function
- correctly does not match instance of DomainA.Created to DomainB.Created
- incorrectly matches instance of DomainA.Created to DomainC.Created
Same result in REPLs for Scala 2.10.2, 2.10.3, 2.10.4 and 2.11.5
lookingForAs
? - Mik378event.getClass
inlookingForAs
prints simplyDomain$Created
for any origin ofevent
for me. Does it print different things in your case? - Kolmarevent.getClass
inlookingForAs
printsDomain$Created
for any origin ofevent
and whether or not theDomainA
/DomainB
declarations are inside or outside of the unit test class - jmcnulty