Nasal library: Difference between revisions

Jump to navigation Jump to search
(→‎systime(): Fix example)
(→‎Extension functions: Doc thisfunc())
Line 2,104: Line 2,104:
var t2 = systime(); # record new time
var t2 = systime(); # record new time
print("myFunc() took ", t2 - t, " seconds"); # print result
print("myFunc() took ", t2 - t, " seconds"); # print result
}}
=== thisfunc() ===
{{Nasal doc
|syntax = thisfunc();
|source = {{fgdata file|Nasal/globals.nas|t=Source}}
|text = Returns the function from which this function is called. This allows a function to reliably and safely call itself from within a closure.
|example1 = var stringify_vec = func(input){
    if (typeof(input) == "scalar"){
        return sprintf("%s", input);
    } elsif (typeof(input) == "vector") {
        if (size(input) == 0) return "[]";
        var this = thisfunc();
        var buffer = "[";
        for(var i = 0; i < size(input); i += 1){
            buffer ~= this(input[i]);
            if (i == size(input) - 1) {
                buffer ~= "]";
            } else {
                buffer ~= ", ";
            }
        }
        return buffer;
    } else {
        die("stringify_vec(): Error! Invalid input. Must be a vector or scalar");
    }
}
var test_vec = ["a", "b", "c", 1, 2, 3];
debug.dump(stringify_vec(test_vec)); # prints "[a, b, c, 1, 2, 3]"
test_vec = [];
debug.dump(stringify_vec(test_vec)); # prints "[]"
test_vec = {};
debug.dump(stringify_vec(test_vec)); # will throw an error
}}<!--
}}<!--


Navigation menu