简体   繁体   中英

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?

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]
}

Try this:

  trait Event
  case class Event1[A](a: A) extends Event
  case class Event2[A, B](a: A, b: B) extends Event

  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]]

It compiled for me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM