Skip to content

Disallow _ as an identifier #10384

Closed
Closed
@szeiger

Description

@szeiger

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:

val _ = 1 // Identifier, even though it could also be a simple pattern
var _ = 1 // The same
def _ = 1 // Syntax error
println(_) // Eta expansion

Implicit vals are a dangerous corner case where the difference between an identifier and a simple pattern becomes important:

implicit val (_, _) = (42, "foo") // _ treated as simple pattern
implicitly[Int] // Does not compile

implicit val _ = 42 // _ treated as identifier
implicitly[Int] // returns 42

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).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions