Search code examples
scalagenericstypesgeneric-programming

Common supertype of methods


Consider the following definitions:

trait Event
case class Event1[A] extends Event
case class Event2[A, B] extends Event
/* ... */

trait Filter { val cond: Event => Boolean }
case class Filter1[A](cond: Event1[A] => Boolean) extends Filter
case class Filter2[A, B](cond: Event2[A, B] => Boolean) extends Filter
 /* ... */

I think it is quite clear what I am trying to accomplish here: I want to make sure that whenever I encounter a Filter, it is guaranteed to have a cond function that takes the respective subtype of Event and gives me a Boolean. Obviously, the code above doesn't compile as, for example, Event1[A] => Boolean is not really a subtype of Event => Boolean. How would one solve such an issue?


Solution

  • How about something like the following?

    sealed trait Event
    case class Event1[A]() extends Event
    case class Event2[A, B]() extends Event
    /* ... */
    
    sealed trait Filter[T <: Event] { val cond: T => Boolean }
    case class Filter1[A](cond: Event1[A] => Boolean) extends Filter[Event1[A]]
    case class Filter2[A, B](cond: Event2[A, B] => Boolean) extends Filter[Event2[A, B]]
    

    Alternatively, you could override an abstract type instead of using parametrized types:

    sealed trait Filter {
      type Filterable
      val cond: Filterable => Boolean
    }
    case class Filter1[A](cond : Event1[A] => Boolean) extends Filter{
      override type Filterable = Event1[A]
    }
    
    case class Filter2[A, B](cond: Event2[A, B] => Boolean) extends Filter{
      override type Filterable = Event2[A, B]
    }