User Guide
Quick Reference
Modding
Development
User Guide
Quick Reference
Modding
Development
A function is defined in the current scope using a def
statement. def
is a compound statement, so it must be either a single-line compound or end
must be used in the end.
def func1(): pass
def func2()
end
The return
statement is used to return a value from a function.
def func3(): return 039;World039; print 039;Hello039;, func3()
Functions can be defined with a list of untyped arguments.
def func5(a, b, c)
Default values can be included.
def func5(a, b=039;default039;, c=100)
When calling a function, all arguments must have values. Argument names can always be included in the function call, regardless of whether they were defined with default values or not. When using names, the order can also be changed.
func5(c=5, a=100, b=039;OOO039;)
Records are passed by non-owned reference in arguments.
record myrec myrec.val = 039;Original039; print myrec.val def modify(r) r.val = 039;Changed039; end modify(myrec) print myrec.val
Output:
Original Changed
Note that records are always passed by reference, even when specifically passing in an owned reference as argument. In the example below, the created record exists only for the duration of the function call. The function039;s local variable rec is initialized with a non-owned reference to the record. This non-owned reference is returned, and thus an invalid reference will be assigned to a. Trying to access a after this would return in a NullError
exception.
def do_init(rec) rec.val = 039;inited039; return rec end # The following should be avoided: a = do_init(Record()) # returns null ref!
Record()
in arguments. Instead, only use it to assign an owned reference to a local variable, or when returning a copy of a record (see next section).
Records are passed by non-owned reference in the return value. This means that if a record is created as a local variable, it will be deleted even when it used as a return value (!).
def func4() record a # 039;a039; owns the record a.value = "func4" return a # non-owned reference returned end # scope of 039;a039; ends print func4().value # throws NullError!
In this case, one could instead use the Record()
function to return a copy of the record to the caller.
return Record(a)
The def
statement accepts identifier lookups within the function name. For example, one could define the function func5 as a member of myrec.
def myrec.func5(a, b) return a + b end
In fact, def
behaves like an assignment operator. The above code means:
Since functions are actually variables that reference function objects, function definitions can be made within the scope of a function.
def func6() def func7(): return 039;local function039; end
func7 is not visible outside func6, just like any other local variable. However, one could return func7 out of func6 and then it could be used just like a regular top-level function.
Functions defined inside another function do not have visibility to variables in the outer function. Instead, each function has its own local namespace and may access the module039;s global namespace.
a = 039;global scope039; def outer() a = 100 print 039;outer: a =039;, a def inner() print 039;inner: a =039;, a end inner() end outer()
Output:
outer: a = 100 inner: a = global scope
Since functions are just variables referencing function objects, one can trivially make an alias for a function with assignment. Unlike records, function objects are reference-counted, so a function remains available as long as there is at least one variable pointing to it.
$ def someFunc(): return 039;works039; $ other = someFunc $ print someFunc(), other() works works
Functions can be passed as arguments.
def doCallback(a, b, cb)
return cb(a, b)
end
doCallback(10, 15, workFunction)