-
Notifications
You must be signed in to change notification settings - Fork 1.1k
An alternative scheme for precise apply methods of enums #9932
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
Conversation
656304b
to
d1d8d45
Compare
one thing that is possible with #9922 but does not work here is: enum Nat:
case Z
case S[N <: Z.type | S[_]](pred: N)
import Nat._
val two = S(S(Z))
// ^^^^
// Found: Nat
// Required: (Nat.Z : Nat) | Nat.S[?] this seems strange because the type parameter is instantiated to |
Keeps many elements from scala#9922 but the modality where we do the widening is different. The new rule is as follows: In an application of a compiler-generated apply or copy method of an enum case, widen its type to the underlying supertype of the enum case by means of a type ascription, unless the expected type is an enum case itself.
Widen whenever it is possible to do so while still conforming to expected type.
d1d8d45
to
8e2aae2
Compare
Good point. This should be fixed in the latest commit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should patch https://github.com/lampepfl/dotty/blob/master/docs/docs/reference/enums/adts.md to update the section about "That is, the implementation case classes are not visible in the result types of their apply methods."
but I would be happy for this to go through. I think the "widen everywhere" case should maybe be revisited if type inference is improved
I have updated the docs now. Better to do it all in one PR. |
def isEnumApply = sym.name == nme.apply && sym.owner.linkedClass.isEnumCase | ||
if sym.is(Synthetic) && (isEnumApply || isEnumCopy) | ||
&& tree.tpe.classSymbol.isEnumCase | ||
&& tree.tpe.widen.isValueType |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this check needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So that we don't widen partially applied applications.
Keeps many elements from #9922 but the modality where we do the
widening is different. The new rule is as follows:
In an application of a compiler-generated apply or copy method of an enum case,
widen its type to the underlying supertype of the enum case by means of a type
ascription as long as the widened type is still compatible with the expected type.