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

Multiple dummy bindings using underscore not allowed #7691

Closed
scabug opened this Issue Jul 23, 2013 · 3 comments

Comments

Projects
None yet
3 participants
@scabug

scabug commented Jul 23, 2013

These show that the compiler knows that a wildcard is not a valid identifier in accordance with the spec:

scala> def _ = 3
<console>:1: error: identifier expected but '_' found.
       def _ = 3
           ^

scala> class _
<console>:1: error: identifier expected but '_' found.
       class _
             ^

scala> val _ = 3
<console>:5: error: identifier expected but '_' found.
  lazy val $result = _
                                          ^
<console>:10: error: identifier expected but '_' found.
 + "_: Int = " + scala.runtime.ScalaRunTime.replStringOf(_, 1000)
                                                                              ^

However, scalac complains about multiple bindings to _ :

scala> { val _ = 3; val _ = 5; 7 }
<console>:8: error: _ is already defined as value _
              { val _ = 3; val _ = 5; 7 }
                               ^

This was reported on a mailing list a while ago (http://www.scala-lang.org/node/7159), but never filed as an issue. One improvement since then:

scala> { val _ = 3; `_` }
<console>:1: error: wildcard invalid as backquoted identifier
       { val _ = 3; `_` }
                    ^
@scabug

This comment has been minimized.

scabug commented Jul 23, 2013

Imported From: https://issues.scala-lang.org/browse/SI-7691?orig=1
Reporter: @harrah
Affected Versions: 2.10.2, 2.11.0-M4

@scabug

This comment has been minimized.

scabug commented Jul 23, 2013

@paulp said:
Also from the world of poorly chosen ways to put _ to work is

object Test {
  def f[_](x: Int) = x      // compiles
  def g[_, _](x: Int) = x   // fails
  // ./a.scala:5: error: _ is already defined as type _
  //   def g[_, _](x: Int) = x
  //            ^
  // one error found
}
@scabug

This comment has been minimized.

scabug commented Aug 17, 2013

@Atry said (edited on Aug 17, 2013 5:51:33 AM UTC):
According to Scala Language Specification, {code} { val _ = "foo"; val _ = "bar" } {code} should be treated as valid , because _ is not a simple identifier but a pattern.

In Scala Language Specification 4.1 Value Declarations and Definitions

If p is some pattern other than a simple name or a name followed by a colon and a type,
then the value definition val p = e is expanded as follows:

  1. If p has no bound variables:
e match { case p => ()}

In Scala Language Specification 1.1 Identifiers

The following names are reserved words instead of being members of the syntactic class id of lexical identifiers.

abstract case catch class def
do else extends false final
finally for forSome if implicit
import lazy match new null
object override package private protected
return sealed super this throw
trait try true type val
var while with yield
_ : = => <<:
<% >: # @

However, scalac should never accept { def _ = "foo"; def _ = "bar" }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment