Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is recursive re declaration allowed? #96

Closed
mikedanese opened this issue Jan 8, 2016 · 2 comments
Closed

Is recursive re declaration allowed? #96

mikedanese opened this issue Jan 8, 2016 · 2 comments

Comments

@mikedanese
Copy link
Contributor

local lib  = {
    f1(a)::
        local a = true;
        a,
    f2(a)::
        local a = a && true;
        a,
    f3(a)::
        local a = true;
        local a = false;
        a,
    f4(a)::
        local b = a && true;
        local a = b;
        a,
};

lib.f1(false) &&
lib.f2(false) &&
lib.f3(false) &&
lib.f4(false) &&
true

Both f1 and f3, f4 work so it seems like f2 should work. Is recursive redecleration explicitly forbidden? It seems quite useful (e.g.) :

local result =
    if condition then
        result + { "foo" : "bar" }
    else
        result
...
result

The error is a RUNTIME error and it should probably be a STATIC error if this explicitly forbidden.

RUNTIME ERROR: Max stack frames exceeded.
        test_suite/redeclare.jsonnet:3:19       thunk <a>
        test_suite/redeclare.jsonnet:3:19       thunk <a>
        test_suite/redeclare.jsonnet:3:19       thunk <a>
        test_suite/redeclare.jsonnet:3:19       thunk <a>
        test_suite/redeclare.jsonnet:3:19       thunk <a>
        test_suite/redeclare.jsonnet:3:19       thunk <a>
        test_suite/redeclare.jsonnet:3:19       thunk <a>
        test_suite/redeclare.jsonnet:3:19       thunk <a>
        test_suite/redeclare.jsonnet:3:19       thunk <a>
        test_suite/redeclare.jsonnet:3:19       thunk <a>
        ...
        test_suite/redeclare.jsonnet:3:19       thunk <a>
        test_suite/redeclare.jsonnet:3:19       thunk <a>
        test_suite/redeclare.jsonnet:3:19       thunk <a>
        test_suite/redeclare.jsonnet:3:19       thunk <a>
        test_suite/redeclare.jsonnet:3:19       thunk <a>
        test_suite/redeclare.jsonnet:3:19       thunk <a>
        test_suite/redeclare.jsonnet:3:19       thunk <a>
        test_suite/redeclare.jsonnet:3:19       thunk <a>
        test_suite/redeclare.jsonnet:4:9        function <anonymous>
        test_suite/redeclare.jsonnet:7:1-13
@sparkprime
Copy link
Contributor

Since this is a lazy language, the second a in

local a = a && true;

is actually referring recursively to the a that is being defined on the same line, so the infinite loop is expected behavior.

You can of course write one that terminates:

local x = false && x

In this case the shortcut semantics for && prevents the x from being recursively evaluated.

This is the same as e.g. Haskell. In fact I think even in C you can do int x = x + 1 although it's undefined behavior because rather than being a recursive definition, you're reading x before it's been initialized.

@mikedanese
Copy link
Contributor Author

I see that it is interpreted as a declaration and not a reassignment which is strange from my naive perspective as it seems like the language then must allow redecleration within a scope just not recursive decleration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants