Skip to content

Support Mirror.SumOf synthesis for AnyVal-based sealed hierarchies #124

Description

@halotukozak

While integrating commons's ToExprFactory into alpaca, I hit a hierarchy that the stock scala.deriving.Mirror (and therefore any Mirror-based derivation, made included) refuses to synthesize a sum mirror for:

private[parser] sealed trait ParseAction extends Any

private[parser] object ParseAction:
  sealed case class Shift(state: Int) extends AnyVal with ParseAction
  sealed case class Reduction(production: Production) extends AnyVal with ParseAction

ParseAction is a "universal trait" (extends Any) so that its case classes can be AnyVals (avoid boxing for what's a hot path in the generated parse table). Trying derives ToExprFactory (which needs Mirror.Of[ParseAction]) fails with:

No given instance of type scala.deriving.Mirror.Of[alpaca.internal.parser.ParseAction] was found
	* trait ParseAction is not a generic product because it is not a case class
	* trait ParseAction is not a generic sum because its child class Shift is not a generic sum because it is not an abstract class

That second bullet reads like the sealed-sum check is trying to validate each child as if it also needed to be sum-derivable, and an AnyVal case class trips that check somewhere it shouldn't.

Since made's whole point is to go beyond what stock Mirror can express, it'd be great if Made.derived[T] could handle this shape (sealed trait extending Any, AnyVal case class children) even where stock Mirror.SumOf can't. Concretely this would let ParseAction drop its hand-written ToExpr instance once commons's ToExprFactory can build on made instead of stock Mirror.

Reference: alpaca's hand-written instance this would replace — src/alpaca/internal/parser/ParseAction.scala.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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