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
Parse +_
and -_
in types as identifiers under -Xsource:3
to support Scala 3.2 placeholder syntax
#9605
Parse +_
and -_
in types as identifiers under -Xsource:3
to support Scala 3.2 placeholder syntax
#9605
Conversation
…eholder syntax This change allows `kind-projector` plugin to rewrite `+_` and `-_` tokens to type lambdas, in line with proposed syntax for Scala 3.2 in http://dotty.epfl.ch/docs/reference/changed-features/wildcards.html When used in conjunction with `-Xsource:3` this will let the user use `?` for wildcards and `_` for placeholders, letting the user cross-compile the same sources with Scala 3 with `-source:3.2` flag. This change is not source breaking since currently `+_` and `-_` fail to parse entirely, this change also does not allow the user to declare types with these names without backticks, they can only be used as part of a type tree.
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.
This change allows kind-projector plugin to rewrite +_ and -_ tokens to type lambdas,
in line with proposed syntax for Scala 3.2 in http://dotty.epfl.ch/docs/reference/changed-features/wildcards.html
This page mentions _
but not +_
or -_
, and I don't believe the latter two will become syntax in a future Scala 3 release, because we already disallowed variance annotations on type lambdas:
class Foo[F[+_]]
def foo(x: Foo[[+X] =>> Either[Int, X]]): Unit = {}
// ^
// no `+/-` variance annotation allowed here
This was disallowed because the compiler now infers the variance of a type lambda from its body, so the following works:
def foo(x: Foo[[X] =>> Either[Int, X]]): Unit = {}
Therefore in the future we can expect the following syntax to work too:
def foo(x: Foo[Either[Int, _]]): Unit = {}
But not a hypothetical syntax with +_
.
I was not aware of this change! However, they can probably be allowed to parse and be treated as just The other glaring thing I was not aware of is that dotty doesn't actually implement underscore syntax right now. I was fooled by scastie! It was silently crashing on old versions of dotty when flag |
That's one possibility, but it seems weird to me to introduce a new syntax that never existed in kind-projector under |
It seems weird now, but if this PR gets merged, then kind-projector will have such syntax – Scala 2 projects in the wild will use it and then this rule relaxation in |
It looks like this isn't actually gatewayed by You could argue that it doesn't matter, but especially since the new syntax is a no-op, it feels weird to me to simply accept it by default; I expected to have to opt-in. |
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.
Complicated matters, but I think I've understood the situation so this change LGTM.
@smarter am I right in taking your merging of the Dotty PR as agreeing to this change too?
The Dotty PR was about |
Good point. With So basically it only makes sense if both this PR and a Scala 3 "be allowed to parse and be treated as just _ under -Ykind-projector for compatibility" PR is merged, right? |
Note that the timeline in http://dotty.epfl.ch/docs/reference/changed-features/wildcards.html is just there to give a rough idea of how things could go, it's not a fixed plan (in particular we may bump our minor version at any time just to signal that we broke forward binary compatibility).
Yep. |
My reasoning for not gating was that it's not directly related to
Yes, the timeline assumes a period when
Well, I assumed Scala 3 already implements underscores, but I was wrong. @smarter I can submit a quick PR that will allow use of By the way I've added the change to kind-projector typelevel/kind-projector#188 & verified that it works for my usecase 7mind/izumi#1479 |
kind-projector repurposing |
I thought Dotty's For Scala 2 with typelevel/kind-projector#188 it repurposes
And if you pass no flags then nothing changes (And you'll get new syntax only in Scala 3.2/3.3 when it becomes the default) |
It only deprecates it for now.
That could work for now yeah. |
Got it, what about a new -Y flag, such as I think that |
Honestly not sure, t'm wary of multiplying the flags since it makes everything more complicated, I guess we could go for the sub-option for now. |
@smarter Submitted at scala/scala3#12378 |
…ption to existing code
I've decided to gate the change behind |
Resonates with me :-) @neko-kai could you take this on? Would be really helpful to have a page that explains the status quo, the goal, and all the possible paths through the forest of combinations of
@smarter where should that documentation go? |
Great question, maybe a dedicated page in the migration guide ? /cc @adpi2 |
@lrytz I've documented the Dotty part in the "Changes to wildcards", I've also expanded kind-projector README in the related kind-projector PR. |
Since this is mainly about migration to Scala 3, I think the migration guide is a good place. We can always link to it from elsewhere. It would be really good to have it all in one place.
Where's that?
|
https://github.com/lampepfl/dotty/pull/12378/files#diff-08f1a956e9a74ed84ae35bea06852879c16776956f075ec5f5b5748e117c6ecaR46 Yep |
Got a verbal ok for that Scala 3 part being acceptable, so that's a re-✅ from me. |
Migration guide updated at scalacenter/scala-3-migration-guide#203 |
+_
and -_
in types as identifiers to support Scala 3.2 placeholder syntax+_
and -_
in types as identifiers under -Xsource:3
to support Scala 3.2 placeholder syntax
This change allows
kind-projector
plugin to rewrite+_
and-_
tokens to type lambdas,in line with proposed syntax for Scala 3.2 in http://dotty.epfl.ch/docs/reference/changed-features/wildcards.html
See also https://scalacenter.github.io/scala-3-migration-guide/docs/tutorials/kind-projector.html (Soon on https://docs.scala-lang.org/scala3/guides/migration/compatibility-intro.html)
When used in conjunction with
-Xsource:3
this will let the user use?
for wildcards and_
for placeholders, letting the user cross-compile the same sources with Scala 3 with-source:3.2
flag.This change is not source breaking since currently
+_
and-_
fail to parse entirely,this change also does not allow the user to declare types with these names without backticks,
they can only be used as part of a type tree.
This does not add a special error when these symbols are used without kind-projector plugin – just "identifier not found", which is however in line with the behavior of other symbols currently used as placeholders in kind-projector –
?
&*
See also:
kind-projector
repository Scala 3 mode with_
as a placeholder and?
as wildcard? typelevel/kind-projector#120