Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ren-C error:; "word is bound relative to context not on stack"

I am using the experimental Ren-C implementation of Rebol3. I can't understand this error:

f: func [x /local y][
    emit: func [x] [y]
    y: 0
    forall x [emit f []]
    0
]

f [0 0]

** Script error: y word is bound relative to context not on stack

** Where: emit forall f do catch either either --anonymous-- do trap either --anonymous--

** Near: ... y

What's wrong with the code?

like image 351
giuliolunati Avatar asked Mar 18 '17 11:03

giuliolunati


1 Answers

This is a by-product of what is known as specific binding, and is behaving as expected.

The issue is that since you are using FUNC instead of FUNCTION for f, emit is not a local of f. Each time you run f, you are overwriting a global emit, while y is local to each specific instantiation.

So the global emit, which gets overwritten on each call, winds up getting a version of the emit function whose concept of y is relative to calls to f which no longer exist.

If you really intend to create a new local to hold a unique function--with a unique concept of y--each time it runs, you may do so explictly:

f: func [x /local y emit][
    emit: func [x] [y]
    y: 0
    forall x [emit f []]
    0
]

f [0 0]

Or implicitly:

f: function [x] [
    emit: func [x] [y]
    y: 0
    forall x [emit f []]
    0
]

f [0 0]
like image 135
HostileFork says dont trust SE Avatar answered Oct 13 '22 04:10

HostileFork says dont trust SE