functions need to be declared before they can be used

Issue #12 wontfix
Fabian Maurer created an issue
function test()
    say "hello";
end
test();

works

test();
function test()
    say "hello";
end

Error: Attempted to call a nil value.

Is this intended ? Because it makes things a lot more complicated.

Comments (5)

  1. Chimaine

    This is indeed intended and is how most imperative scripting languages work.

    Function definitions are essentially a statement where an anonymous function is assigned to a value. If you try to access a value before it is assigned, the logical result is nil.

    //edit: To clarify, asking to make this possible is like asking for

    print( foo );
    foo = "Hello World!";
    

    to print "Hello World!" instead of "nil".

  2. Chimaine

    Out of curiosity, in what situations will this make it more complicated? I worked with Lua for a long time and never encountered a situation where this would complicate anything.

  3. Fabian Maurer reporter

    Well I think I just thought confused something. Thought it would be like in C, that I need to reorder them before I can use them. But that's nonsense, so it's fine. :D

    Though I'd like to know how it will work with multiple script files. One needs to run over each script once to make the functions accessible ?

  4. Chimaine

    Yeah, something like that. Currently still fleshing out the details. Gonna share more at the forums!

  5. Log in to comment