Skip to content

Strange and inconsistent behaviour when using _ as a method name/identifier #322

@IsaacOscar

Description

@IsaacOscar

I noticed that the following program is accepted:

def _ = 1
def _ = 2

But the following is not:

method _ { 1 }
method _ { 2 }

It produces the following error:

a.grace[2:8]: Syntax error: '_' cannot be redeclared because it is already declared as a method on line 1
   1: method _ { 1 }
   2: method _ { 2 }
-------------^

The same also applies to interface { _; _}

I also note that the following code is not a syntax error, but produces a runtime error:

def _ is public = 1
self._

When run

NoSuchMethod: no method _ on the "a" module (defined at a:1).  Did you mean __1?
  raised from module initialization at a:2
minigrace: program a exited with code 3.

(writing self.__1 does work)

After some thought, the only uses I can see for _ is as a parameter name (of a method, block, trait, etc.) or as the RHS of an import (import "foo" as _).
I think all other uses should be syntax errors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions