-
Notifications
You must be signed in to change notification settings - Fork 21
Closed
Description
it is quite common when writing invariant ADTs that have an "empty" element to use something along the lines of the following encoding
sealed abstract class Maybe[A]
final case class Empty[A]() extends Maybe[A]
final case class Just[A](a: A) extends Maybe[A]
but this incurs an object allocation overhead that must be mitigated with the following (ugly) pattern
final case object Empty extends Maybe[Nothing] {
def apply[A](): Maybe[A] = this.asInstanceOf[Maybe[A]]
def unapply[A](e: Maybe[A]): Boolean = this == e
}
it would be fantastic if the compiler automatically added the apply()
and unapply()
under these circumstances.
Metadata
Metadata
Assignees
Labels
No labels