Skip to content

val _ should not create an identifier named "_" #7761

@scabug

Description

@scabug
scala> { val _ = "foo"; val _ = "bar"; }
<console>:8: error: _ is already defined as value _
              { val _ = "foo"; val _ = "bar"; }
                                   ^

The error should not be present, 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:

3. 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
_ : = => <<:
<% >: # @

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions