3
votes

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

1
In which class is defined lookingForAs ? - Mik378
I defined all the types in one object in the same file and I get "Oops matched a B". - M.K.
The above is wrapped in a unit test class except of course for RemoteDomain which is in another file in the same package. I'll try same file/different enclosing object to see if that makes a difference - jmcnulty
Printing event.getClass in lookingForAs prints simply Domain$Created for any origin of event for me. Does it print different things in your case? - Kolmar
Printing event.getClass in lookingForAs prints Domain$Created for any origin of event and whether or not the DomainA/DomainB declarations are inside or outside of the unit test class - jmcnulty

1 Answers

0
votes

Got an answer from scala-users Google Group: Jason Zaugg (retronym) says this is a Scala bug and has created SI-9110 to track it for a fix targeted to 2.12. Thanks again, Jason