Stof Docs
Star on GitHubStof WebsiteDiscordContact Us
  • 🚀Welcome
  • â„šī¸Resources & Information
  • Book
    • Data Interface
    • Introduction & Design
    • Host Environments
      • Rust
      • TypeScript
        • Extend Stof
        • Import Statement
    • Data Interchange
      • JSON to Stof
      • Importing and Parsing Data
      • Exporting Data
      • REST and Restructure
    • Configuration Files
    • Schemas
      • Renaming Fields
      • Removing Fields
      • Validation & Transformation
        • Example Access
      • Nested Schemas
    • Orchestration
  • Common Concepts
    • Objects
    • Primitive Types
    • Functions
    • Fields
    • Object Types
    • Imports
    • Error Handling
    • Units
    • Casting/Conversions
  • Reference
    • CLI
      • 🏃Run
      • đŸ§ĒTest
      • 📚CLI Libraries
        • Filesystem Library
        • Time Library
        • HTTP Library
    • Stof Language
    • Libraries
      • Standard Library
      • Array/Vector Library
      • Number Library
      • String Library
      • Object Library
      • Function Library
      • Set Library
      • Map Library
      • Data Library
      • Tuple Library
      • Blob Library
      • Boolean Library
    • đŸĒ§Formats
Powered by GitBook
On this page
  • Common Value Functions
  • Function.toString(func: fn): str
  • Function.or(func: fn, ...): unknown
  • Library Functions
  • Function.call(func: fn, ...): unknown
  • Function.name(func: fn): str
  • Function.parameters(func: fn): vec
  • Function.returnType(func: fn): str
  • Function.attributes(func: fn): map
  • Function.hasAttribute(func: fn, key: str): bool
  • Function.object(func: fn): obj
  • Function.objects(func: fn): vec

Was this helpful?

  1. Reference
  2. Libraries

Function Library

Stof's standard function library ("Function").

PreviousObject LibraryNextSet Library

Last updated 4 months ago

Was this helpful?

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");
}
Standard Library