Skip to content

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

Merged
merged 5 commits into from
Oct 2, 2020

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Oct 1, 2020

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.

@bishabosha
Copy link
Member

bishabosha commented Oct 1, 2020

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 Nat.S[(Nat.Z : Nat)]

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.
@odersky odersky force-pushed the change-enum-apply branch from d1d8d45 to 8e2aae2 Compare October 2, 2020 07:54
@odersky
Copy link
Contributor Author

odersky commented Oct 2, 2020

one thing that is possible with #9922 but does not work here is:

Good point. This should be fixed in the latest commit

Copy link
Member

@bishabosha bishabosha left a 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

@odersky
Copy link
Contributor Author

odersky commented Oct 2, 2020

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
Copy link
Member

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?

Copy link
Contributor Author

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.

@odersky odersky merged commit 371d8f5 into scala:master Oct 2, 2020
@odersky odersky deleted the change-enum-apply branch October 2, 2020 14:44
@Kordyjan Kordyjan added this to the 3.0.0 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants