Skip to content

case class optimisation #10659

Closed
Closed
@fommil

Description

@fommil

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions