Skip to content

Specify order of MirroredElemTypes for Mirror.SumOf #8390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/docs/reference/contextual/derivation.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ Note the following properties of `Mirror` types,
Scala 2 versions of shapeless). Instead the collection of child types of a data type is represented by an ordinary,
possibly parameterized, tuple type. Dotty's metaprogramming facilities can be used to work with these tuple types
as-is, and higher level libraries can be built on top of them.
+ For both product and sum types, the elements of `MirroredElemTypes` are arranged in definition order (i.e. `Branch[T]`
precedes `Leaf[T]` in `MirroredElemTypes` for `Tree` because `Branch` is defined before `Leaf` in the source file).
This means that `Mirror.Sum` differs in this respect from shapeless's generic representation for ADTs in Scala 2,
where the constructors are ordered alphabetically by name.
+ The methods `ordinal` and `fromProduct` are defined in terms of `MirroredMonoType` which is the type of kind-`*`
which is obtained from `MirroredType` by wildcarding its type parameters.

Expand Down
24 changes: 24 additions & 0 deletions tests/run/deriving-constructor-order.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import scala.compiletime.erasedValue
import scala.deriving.Mirror

object Test extends App {
inline def checkElems[A, T](using inline A: Mirror.SumOf[A]): Unit =
inline erasedValue[A.MirroredElemTypes] match {
case _: T => ()
}

sealed trait Base1
case class Foo() extends Base1
case object Bar extends Base1
case class Qux(i: Int) extends Base1

checkElems[Base1, (Foo, Bar.type, Qux)]

enum Tree[+T] {
case Empty
case Branch[T](left: Tree[T], right: Tree[T]) extends Tree[T]
case Leaf[T](elem: T) extends Tree[T]
}

checkElems[Tree[String], (Tree.Empty.type, Tree.Branch[String], Tree.Leaf[String])]
}