For information / discussion: Add support for Scala 3 enums#1176
Open
DavidGregory084 wants to merge 1 commit intoplayframework:mainfrom
Open
For information / discussion: Add support for Scala 3 enums#1176DavidGregory084 wants to merge 1 commit intoplayframework:mainfrom
DavidGregory084 wants to merge 1 commit intoplayframework:mainfrom
Conversation
DavidGregory084
commented
Jun 25, 2025
| if types.isEmpty then None else Some(types) | ||
| tpr.asType match { | ||
| case '[t] => | ||
| Expr.summon[Mirror.Of[t]].collect { |
Author
There was a problem hiding this comment.
I think that a less WIP implementation of this should probably use Implicits.search so that it could surface any implicit search error messages to the user
DavidGregory084
commented
Jun 25, 2025
|
|
||
| val tpeCaseName: Expr[String] = '{ | ||
| ${ config }.typeNaming(${ Expr(typeName(tpr.typeSymbol)) }) | ||
| ${ config }.typeNaming(${ Expr(typeName(if isCase then termSym else typeSym)) }) |
Author
There was a problem hiding this comment.
By using the termSymbol we get the fully qualified name of the enum case rather than the name of the enum as our type discriminator.
DavidGregory084
commented
Jun 25, 2025
| tpd.tpt.tpe | ||
|
|
||
| case vd: ValDef => | ||
| vd.tpt.tpe |
Author
There was a problem hiding this comment.
I don't know what to do here to get the singleton Type that the compiler-generated Mirror gives us
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request Checklist
Fixes
Fixes #1017.
However, it does so using Scala 3's Mirror.Sum. This forbids any subclasses which are not products or sums themselves.
This is why the test for
knownSubclassesis failing -TestUnion.UThas a subclassUCwhich is a normal class.Purpose
This PR adds support for Scala 3
enums.Background Context
The problem with the current implementation is that it assumes that the
.childrenof aTypeReprwill be subclasses of thatTypeRepr.Scala 3
enumcases are encoded asvals which are bound to an anonymous class. They are ascribed with the type of the parent definition.For example:
This causes the
subclassesfunction of theknownSubclasseshelper to loop indefinitely trying to find the subclasses ofColorbecause thetpt.tpeof theValDefchildren is the sameTypeReprforColorthat we started with. This is the cause for #1019.I do not know how to get the singleton
TypeReprof theenumcasefrom theValDef, but I think that would be the way to solve this problem while preserving the existing behaviour of the macro.References
Scala 3 Enumerations Implementation