-
Notifications
You must be signed in to change notification settings - Fork 21
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
Disallow _ as an identifier #10384
Comments
As another datapoint, |
Underscore is lower case. scala/scala#5919 It's underscore, not upperscore or overscore. Also #6426 I was going to say:
but I guess I won't. (The import is not relevant to the crash.) Wasn't there a movement underfoot or perhaps a motion for underscore to introduce a fresh name? with caveats around stability of member names. I may be conflating underscore with the consultancy.
They're going to make us supply types of implicits, must we supply names, too? |
Using underscores to not introduce a name is essential for un-imports and very convenient for pattern matching. You wouldn't want to bind lots of vals you don't care about. For consistency it would be nice to get the same behavior in other cases as well, rather than introducing fresh names (or binding to
In Scala 2.12 this doesn't bind to
For some reason Dotty doesn't like a quoted
But by writing an implicit function we can observe that the implicit parameter is indeed bound to a name:
|
BTW, accidental assignment to No problem here:
Here the REPL tries to create code to print the result (and fails):
|
In case it is decided not to disallow _ as an identifier I extracted the REPL bug into it's own issue: #10645. |
@szeiger Is it possible to extend this behavior to support object Implicits {
// Generate a term name from the method type
implicit def _: Ordering[BigInteger] = ???
}
import Implicits._
implicitly[Ordering[BigInteger]] |
The canonical ticket is #7691 The anon fun syntax is covered by 6.23, which says you get a fresh name for underscore; but it doesn't include implicit underscore; maybe that was added by popular demand? The other "fresh name" semantics is what I asked about in a previous comment. Maybe that will also succumb to popular demand. After the # metoo movement, I believe that the people have the power. |
For public methods, "fresh name" may be not a good idea to keep backward compatibility. Hash naming based on the method type may be a better choice. |
I submitted a fix at #7691 I think most of the info is on the other ticket? |
@som-snytt should this ticket still be open? |
@szeiger might have another idea, but I think the edge cases were covered in some form:
I think the speculative part about using underscore to introduce anonymous implicits is still an open issue, but deserves its own ticket if there isn't one. Also, to extend the metaphor, for for:
Also, arguably
|
This project now compiles with Scala 2.13 by default, while still cross-compiling to Scala 2.12. Issues addressed with this upgrade: ### Underscore (`_`) is no longer a valid identifer for vals Using underscore like this is no longer allowed... ``` implicit val _ = ... ``` ...we have to give the `val` a valid name: ``` implicit val v = ... ``` See also: * scala/bug#10384 * scala/bug#7691 * scala/scala#6218 ### `Map.mapValues()` now returns a `MapView` rather than a `Map` We usually want a `Map`, so we can just add in a `.toMap` to the end of the expression - it's harmless in Scala 2.12, and allows the code to compile in Scala 2.13. `Map.mapValues()` itself is deprecated in Scala 2.13, but using the new recommended alternative doesn't work in Scala 2.12. https://docs.scala-lang.org/overviews/core/collections-migration-213.html#what-are-the-breaking-changes See also: * scala/bug#10919 * scala/scala#7014
This project now compiles with Scala 2.13 by default, while still cross-compiling to Scala 2.12. Issues addressed with this upgrade: ### Underscore (`_`) is no longer a valid identifer for `val`s Using underscore like this is no longer allowed... ``` implicit val _ = ... ``` ...we have to give the `val` a valid name (even if it's just `v`): ``` implicit val v = ... ``` See also: * scala/bug#10384 * scala/bug#7691 * scala/scala#6218 ### `Map.mapValues()` now returns a `MapView` rather than a `Map` We usually want a `Map`, so we can just add in a `.toMap` to the end of the expression - it's harmless in Scala 2.12, and allows the code to compile in Scala 2.13. `Map.mapValues()` itself is deprecated in Scala 2.13, but using the new recommended alternative doesn't work in Scala 2.12. https://docs.scala-lang.org/overviews/core/collections-migration-213.html#what-are-the-breaking-changes See also: * scala/bug#10919 * scala/scala#7014 ### `.to` can no longer infer what resulting collection type you want This is all part of `CanBuildFrom` going away in Scala 2.13: https://www.scala-lang.org/blog/2018/06/13/scala-213-collections.html#life-without-canbuildfrom
This project now compiles with Scala 2.13 by default, while still cross-compiling to Scala 2.12 & 2.11. Issues addressed with this upgrade: ### Underscore (`_`) is no longer a valid identifer for `val`s Using underscore like this is no longer allowed... ``` implicit val _ = ... ``` ...we have to give the `val` a valid name (even if it's just `v`): ``` implicit val v = ... ``` See also: * scala/bug#10384 * scala/bug#7691 * scala/scala#6218 ### `Map.mapValues()` now returns a `MapView` rather than a `Map` We usually want a `Map`, so we can just add in a `.toMap` to the end of the expression - it's harmless in Scala 2.12, and allows the code to compile in Scala 2.13. `Map.mapValues()` itself is deprecated in Scala 2.13, but using the new recommended alternative doesn't work in Scala 2.12. https://docs.scala-lang.org/overviews/core/collections-migration-213.html#what-are-the-breaking-changes See also: * scala/bug#10919 * scala/scala#7014 ### `.to` can no longer infer what resulting collection type you want This is all part of `CanBuildFrom` going away in Scala 2.13: https://www.scala-lang.org/blog/2018/06/13/scala-213-collections.html#life-without-canbuildfrom
This project now compiles with Scala 2.13 by default, while still cross-compiling to Scala 2.12 & 2.11. Issues addressed with this upgrade: ### Underscore (`_`) is no longer a valid identifer for `val`s Using underscore like this is no longer allowed... ``` implicit val _ = ... ``` ...we have to give the `val` a valid name (even if it's just `v`): ``` implicit val v = ... ``` See also: * scala/bug#10384 * scala/bug#7691 * scala/scala#6218 ### `Map.mapValues()` now returns a `MapView` rather than a `Map` We usually want a `Map`, so we can just add in a `.toMap` to the end of the expression - it's harmless in Scala 2.12, and allows the code to compile in Scala 2.13. `Map.mapValues()` itself is deprecated in Scala 2.13, but using the new recommended alternative doesn't work in Scala 2.12. https://docs.scala-lang.org/overviews/core/collections-migration-213.html#what-are-the-breaking-changes See also: * scala/bug#10919 * scala/scala#7014 ### `.to` can no longer infer what resulting collection type you want This is all part of `CanBuildFrom` going away in Scala 2.13: https://www.scala-lang.org/blog/2018/06/13/scala-213-collections.html#life-without-canbuildfrom
This project now compiles with Scala 2.13 by default, while still cross-compiling to Scala 2.12 & 2.11. Issues addressed with this upgrade: ### Underscore (`_`) is no longer a valid identifer for `val`s Using underscore like this is no longer allowed... ``` implicit val _ = ... ``` ...we have to give the `val` a valid name (even if it's just `v`): ``` implicit val v = ... ``` See also: * scala/bug#10384 * scala/bug#7691 * scala/scala#6218 ### `Map.mapValues()` now returns a `MapView` rather than a `Map` We usually want a `Map`, so we can just add in a `.toMap` to the end of the expression - it's harmless in Scala 2.12, and allows the code to compile in Scala 2.13. `Map.mapValues()` itself is deprecated in Scala 2.13, but using the new recommended alternative doesn't work in Scala 2.12. https://docs.scala-lang.org/overviews/core/collections-migration-213.html#what-are-the-breaking-changes See also: * scala/bug#10919 * scala/scala#7014 ### `.to` can no longer infer what resulting collection type you want This is all part of `CanBuildFrom` going away in Scala 2.13: https://www.scala-lang.org/blog/2018/06/13/scala-213-collections.html#life-without-canbuildfrom
This project now compiles with Scala 2.13 by default, while still cross-compiling to Scala 2.12 & 2.11. Although Scala 2.13.1 has been released, this change sticks with 2.13.0, as `scoverage` has not yet released an update compatible with 2.13.1: scoverage/sbt-scoverage#295 scoverage/scalac-scoverage-plugin#283 scoverage/sbt-scoverage#299 Migration issues addressed with the update to Scala 2.13: ### Underscore (`_`) is no longer a valid identifer for `val`s Using underscore like this is no longer allowed... ``` implicit val _ = ... ``` ...we have to give the `val` a valid name (even if it's just `v`): ``` implicit val v = ... ``` See also: * scala/bug#10384 * scala/bug#7691 * scala/scala#6218 ### `Map.mapValues()` now returns a `MapView` rather than a `Map` We usually want a `Map`, so we can just add in a `.toMap` to the end of the expression - it's harmless in Scala 2.12, and allows the code to compile in Scala 2.13. `Map.mapValues()` itself is deprecated in Scala 2.13, but using the new recommended alternative doesn't work in Scala 2.12. https://docs.scala-lang.org/overviews/core/collections-migration-213.html#what-are-the-breaking-changes See also: * scala/bug#10919 * scala/scala#7014 ### `.to` can no longer infer what resulting collection type you want This is all part of `CanBuildFrom` going away in Scala 2.13: https://www.scala-lang.org/blog/2018/06/13/scala-213-collections.html#life-without-canbuildfrom
This project now compiles with Scala 2.13 by default, while still cross-compiling to Scala 2.12 & 2.11. Although Scala 2.13.1 has been released, this change sticks with 2.13.0, as `scoverage` has not yet released an update compatible with 2.13.1: scoverage/sbt-scoverage#295 scoverage/scalac-scoverage-plugin#283 scoverage/sbt-scoverage#299 Migration issues addressed with the update to Scala 2.13: ### Underscore (`_`) is no longer a valid identifer for `val`s Using underscore like this is no longer allowed... ``` implicit val _ = ... ``` ...we have to give the `val` a valid name (even if it's just `v`): ``` implicit val v = ... ``` See also: * scala/bug#10384 * scala/bug#7691 * scala/scala#6218 ### `Map.mapValues()` now returns a `MapView` rather than a `Map` We usually want a `Map`, so we can just add in a `.toMap` to the end of the expression - it's harmless in Scala 2.12, and allows the code to compile in Scala 2.13. `Map.mapValues()` itself is deprecated in Scala 2.13, but using the new recommended alternative doesn't work in Scala 2.12. https://docs.scala-lang.org/overviews/core/collections-migration-213.html#what-are-the-breaking-changes See also: * scala/bug#10919 * scala/scala#7014 ### `.to` can no longer infer what resulting collection type you want This is all part of `CanBuildFrom` going away in Scala 2.13: https://www.scala-lang.org/blog/2018/06/13/scala-213-collections.html#life-without-canbuildfrom
Currently the lexical syntax allows a single underscore
_
as a valid identifier because it is treated as an upper-case character. This leads to inconsistencies in the syntax because in many (but not in all) contexts the underscore has special meaning and is therefore not a valid identifier:Implicit vals are a dangerous corner case where the difference between an identifier and a simple pattern becomes important:
I propose to change the lexical syntax so that a single underscore is no longer a valid identifier (unless it is enclosed in backticks). When used in a val or var definition it should be treated as a simple pattern (in the way in which is is already specified).
The text was updated successfully, but these errors were encountered: