Skip to content

Allow variable shadowing in the REPL #2026

Open

Description

Part of #2023.

Issue To Be Solved

Identifiers cannot be rebound in the REPL.

Unlike a regular contract/transaction environment, the REPL workflow is entered line-by-line. Oftentimes when testing one-off expressions, it's useful to be able to repeatedly bind the same name to expressions for rapid iteration.

Suggested Solution

An idea to implement this might be to simply assign an internal unique identifier to each declaration, and keep track of mappings between renamed bindings and user-supplied names. For instance, repeated declarations like

> let x: Int = 0
> let x: String = "hi"
> let x: Int64 = 42

would be treated by the interpreter as separate declarations:

let x0: Int = 0
let x1: String = "hi"
let x2: Int64 = 42

and then subsequent usages of an un-renamed x would look up the current renamed mapping.

// REPL
> let y: Int64 = x * 2
> let x: Int64 = 1337

// internally:
let y: Int64 = x2 * 2 // 84
let x3: Int64 = 42

This prevents rebound variables from affecting previous declarations; that is, shadowing a variable should not affect previous uses.

Error messages would require the inverse operation where the internally-renamed value is rendered as its user-supplied name.

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

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions