Skip to content
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

Allow -_ and +_ as type names for -Xsource3 flag #2348

Merged
merged 1 commit into from May 24, 2021

Conversation

dos65
Copy link
Member

@dos65 dos65 commented May 18, 2021

Addresses #2347

@dos65 dos65 force-pushed the kind-projector-source3 branch 3 times, most recently from 1c3bbdf to 05c354a Compare May 18, 2021 17:28
Copy link
Contributor

@kitbellew kitbellew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for butting in with yet another naming suggestion... @tgodzik @dos65 see if it makes more semantic (rather than syntactic) sense to you, as it did for me.

* works under -Xsource3 flag
* https://github.com/scala/scala/pull/9605
*/
val allowPlusMinusUnderscoreIdent: Boolean
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rename this. Looking at the scala code and doc, I'd call this allowUnderscoreAsTypeWildcard and set to false in scala3 and scala2source3. the plus/minus thing is just a side effect because it needs co/contravariant types.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have allowTypeParamUndesrcore setting

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so why not use it? this is how it is described:

    // Dotty rejects placeholder as Type parameter
    val allowTypeParamUnderscore: Boolean,

yet it is never set to false anywhere and never used in parsing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this one fits better.
But in this case non Source3 scala2 dialects will accept this new syntax too.
Also, I forgot that we also need to enable allowTypeParamUnderscore for scala3

@kitbellew @tgodzik are you ok with that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. allowTypeParamUnderscore is about different thing.

def f[_](a: Int): Int = ???

Ok, then the question is: are you ok to enable this syntax by default without any flag?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. allowTypeParamUnderscore is about different thing.

def f[_](a: Int): Int = ???

Right, it's for a type parameter of a definition, not for a type definition. still, why can't i find any reads for allowTypeParamUnderscore, and never false for it?

Ok, then the question is: are you ok to enable this syntax by default without any flag?

allowUnderscoreAsTypeWildcard (or whatever you decide to call it) shouldn't be part of scala213.

i am thinking that we should implement some sort of reflection to allow a user to specify config

dialect {
  base = scala213
  options {
    allowUnderscoreAsTypeWildcard = true
  }
}

that way, we don't need to add it as part of any explicit dialect, we don't need scala213source3 either.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, it's for a type parameter of a definition, not for a type definition. still, why can't i find any reads for allowTypeParamUnderscore, and never false for it?

It's used in several places. For example - here

What about setting rename. I decided to keep the existing one.
I looked on it one more time and as we currently always parse _ as Type.Placeholder without any settings so the name you proposed doesn't fit for that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about setting rename. I decided to keep the existing one.
I looked on it one more time and as we currently always parse _ as Type.Placeholder without any settings so the name you proposed doesn't fit for that.

Perhaps I should have been clearer. My proposal (with wildcards) would have had the opposite value; i.e., if you [still] allow wildcards, then it will not be parsed with +/-.

autoPos {
accept[Ident]
accept[Underscore]
Type.Name(s"${ident.value}_")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should Type.Placeholder be subclassed or supplemented with Type.CovariantPlaceholder and Type.ContravariantPlaceholder and used here? after all, +_ is closer to a placeholder than an identifier, no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that. However, it's unclear if just a Name or Placeholder.
In Scala2 parser treat it as Name and then if kind-projector is enabled it might be transformed into Placeholder.

In Scala3 it will be Placeholder. Are there any cons of having Placeholder here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

meaning, ignore the covariance (since, according to @neko-kai, it will be inferred) and just pretend it was a simple _? maybe.

Copy link

@neko-kai neko-kai May 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kitbellew In Scala 3 yes. In Scala 2 the variance is meaningful – I believe Scalameta should treat these exactly as it currently treats +*/-*/+?/-? kind projector placeholders right now – regarding the latter, Scala 3's -Ykind-projector also treats +* and -* as if they're a simple _ as well – scala/scala3#12341

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, scalameta parses kind-projector syntax (+*/-*/+?/-?) just as Type.Name.
An additional thing here that we don't have variance mods for Type.Placeholder.

I think the best option here would be to turn +_, -_ into Type.Placeholder for Scala3 and keep them as Type.Name for Scala2*.

@kitbellew @tgodzik wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the best option here would be to turn +_, -_ into Type.Placeholder for Scala3 and keep them as Type.Name for Scala2*.

sounds good

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -152,6 +154,7 @@ package object dialects {
.withAllowStarWildcardImport(true)
.withAllowProcedureSyntax(false)
.withAllowDoWhile(false)
.withAllowPlusMinusUnderscoreIdent(true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we start creating additional scala3 dialects? if we have lots of scala2.1x, why not scala3.1 and scala3.2?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will most likely need to, but currently I am not sure what will be in 3.1 at this point.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we know that this flag we are adding is only in 3.2, right? at least according to the docs.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3.2 is when underscores will become the default syntax for type lambdas, without any flags. (+_ and -_ however will still be guarded by a flag, because they do not make sense for new code, only for cross-compilation, because now variance is inferred)

for kind-projector that works under -Xsource3 flag.
@dos65
Copy link
Member Author

dos65 commented May 24, 2021

Summarizing all comments the final changeset is:

  • new flag allowPlusMinusUnderscoreAsIdent = true for Scala2*Source3
  • new flag allowPlusMinusUnderscoreAsPlaceholder = true for Scala3

@dos65 dos65 requested review from tgodzik and kitbellew May 24, 2021 10:07
Copy link
Collaborator

@tgodzik tgodzik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

if (dialect.allowPlusMinusUnderscoreAsPlaceholder)
Type.Placeholder(typeBounds())
else
Type.Name(s"${ident.value}_")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this version not allow type bounds?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, but it is in line with scala/scala#9605

@dos65 dos65 merged commit b9bd51b into scalameta:main May 24, 2021
@dos65 dos65 deleted the kind-projector-source3 branch May 24, 2021 13:19
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.

None yet

4 participants