Common Value Functions
Function.toString(func: fn): str
Returns a string with this function's ID, helpful at times for debugging.
Function.or(func: fn, ...): unknown
Returns the first non-empty (null or void) argument, just like the "or" function.
#[test]
fn test() {
let func = self.dne.or((): str => "hey");
assertEq(func.call(), "hey");
}
Library Functions
Function.call(func: fn, ...): unknown
Call this function, passing in any arguments given, in order.
fn add(x: float, y: float): float { return x + y; }
#[test]
fn test() {
let func = self.add;
assertEq(func.call(2, 2), 4);
assertEq(Function.call(func, 10, 10), 20);
func = (name: str): str => `Hello, ${name}!`;
assertEq(func.call("Melissa"), "Hello, Melissa!");
}
Function.name(func: fn): str
Return the name of this function.
fn add(x: int, y: int): int { return x + y; }
#[test]
fn test() {
assertEq(self.add.name(), "add");
}
Function.parameters(func: fn): vec
Returns a vector containing this function's parameters (a Tuple of (name: str, type: str)
).
fn add(x: int, y: int): int { return x + y; }
#[test]
fn test() {
assertEq(self.add.parameters(), [("x", "int"), ("y", "int")]);
}
Function.returnType(func: fn): str
Returns this function's return type (typeof).
fn add(x: int, y: int): int { return x + y; }
#[test]
fn test() {
assertEq(self.add.returnType(), "int");
}
Function.attributes(func: fn): map
Returns a map of the attributes that exist on this function.
#[custom(2)]
#[test]
#[main]
fn main() {
let attrs = self.main.attributes();
assertEq(attrs.keys(), ["custom", "main", "test"]);
}
Function.hasAttribute(func: fn, key: str): bool
Returns true if this function has an attribute with the given key.
#[custom(2)]
#[test]
#[main]
fn main() {
assert(self.main.hasAttribute("test"));
}
Function.object(func: fn): obj
Return the first object that this function is attached to.
fn mul(a: float, b: float): float { return a * b; }
#[test]
fn test() {
assertEq(self.mul.object(), root);
}
Function.objects(func: fn): vec
Return a vector of all of the objects that this function is attached to.
a: {
fn hello(): str { return "hello"; }
}
b: {
ref root.a.hello; // syntax for Object.reference(obj, path: str)
}
#[test]
fn test() {
assertEq(self.a.hello.objects().len(), 2);
assertEq(self.a.hello(), "hello");
assertEq(self.b.hello(), "hello");
}