acton icon indicating copy to clipboard operation
acton copied to clipboard

Allow forward references to functions

Open plajjan opened this issue 4 months ago • 1 comments

This is a suggestion to implement program reordering of functions in order to allow writing async callback style programs in a more natural way. It spawned out of https://github.com/actonlang/acton/issues/1417 and specifically the comment https://github.com/actonlang/acton/issues/1417#issuecomment-1678851043.

@nordlander had an idea that we can reorder the program. All functions can be moved as far as possible up in their current scope. As far as possible means up to their first dependency, so if a function references a variable assigned outside of it, then it can be moved till just under it:

actor Foo():
    var apa = 1
    var bepa = 2
    var cepa = some_fun()

    def some_fun():
        return apa + 1

the above currently fails, because we try to call some_fun() before it has been defined. With this suggestion, we could move it upwards. some_fun() uses apa internally and thus depend on it, so we cannot move it further up than just under apa, like so:

actor Foo():
    var apa = 1

    def some_fun():
        return apa + 1

    var bepa = 2
    var cepa = some_fun()

and that is high enough, it's obvious how it now is visible to the line var cepa = some_fun().

@nordlander this is per your suggestion, right?

plajjan avatar Aug 20 '25 13:08 plajjan

Exactly!

nordlander avatar Aug 20 '25 19:08 nordlander