Consider subtle scoping/shadowing rules #65

Closed
opened 2024-06-04 19:52:36 +00:00 by scott · 1 comment
Owner

Consider the following code:

fn myadd (x, y) -> add (x, y)
let add = add (1, _)
add (2) &=> panic! wrong number of args

This is because, once the name add is shadowed, it clobbers the prelude function add, and expects only one argument. So when myadd is called, the add in its body resolves to the local function add, not the prelude function add.

It's bad practice to shadow prelude functions, but it is possible.

The reason for late-bound local context is, I suppose, for mutual recursion. But since we now require forward declarations, perhaps we freeze the context when we make the function.

Consider the following code: ``` fn myadd (x, y) -> add (x, y) let add = add (1, _) add (2) &=> panic! wrong number of args ``` This is because, once the name `add` is shadowed, it clobbers the prelude function `add`, and expects only one argument. So when `myadd` is called, the `add` in its body resolves to the local function `add`, not the prelude function add. It's bad practice to shadow prelude functions, but it is possible. The reason for late-bound local context is, I suppose, for mutual recursion. But since we now require forward declarations, perhaps we freeze the context when we make the function.
scott added this to the 0.2.0 milestone 2024-06-04 19:52:36 +00:00
scott added the
question
semantics
ux
labels 2024-06-04 19:52:36 +00:00
Author
Owner

Just did it: copied the context the function closes over into a struct, never worry about this again.

Had to do some fancy footwork with forward declarations, but it the solution works!

a25ece5a68

Just did it: copied the context the function closes over into a struct, never worry about this again. Had to do some fancy footwork with forward declarations, but it the solution works! https://alea.ludus.dev/twc/ludus/commit/a25ece5a6889d96d5b24eec267776a10976c6dad
scott closed this issue 2024-06-04 20:07:07 +00:00
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: twc/ludus#65
No description provided.