Quirrel4.41.0

debug.get_function_decl_string

Defined in module "debug"
from "debug" import get_function_decl_string
get_function_decl_string(func: function): string|null

Returns a function declaration string

Parameters

funcfunctionthe function to describe

Return value

The same kind of string this site prints above every symbol page, such as add(a, b: int): any.

For a script closure the string is always rebuilt from the function's own signature. For a native closure that was registered with a declaration string, this returns that exact string; one that was registered with a type mask instead gets a string rebuilt from the mask, with placeholder names arg1, arg2 standing in for the real parameter names, which the mask does not carry.

When SQ_STORE_DOC_OBJECTS is disabled, native declaration text is not retained. Native declarations then use the rebuilt form, which cannot keep declaration-only parameter names, default values, or the declared return type.

Errors

func must already be a closure or a native closure; passing anything else fails the parameter type check before this function runs.

Notes

A script closure always gives a string. A native closure gives a string when its binding registered a declaration string, or when it has a name to rebuild one from. A native closure with no declaration string and no name, such as one made with sq_newclosure and never named, gives null. Check for null before using the result on a native closure.

Example

examples/debug/get_function_decl_string.nut
from "debug" import get_function_decl_string

function add(a, b: int) { return a + b }
println("get_function_decl_string(add) =", get_function_decl_string(add))

// array.insert has no decl string on record, so one is rebuilt from its type
// mask, using arg1, arg2 in place of the real parameter names
println("get_function_decl_string([].insert) =", get_function_decl_string([].insert))

try {
  get_function_decl_string("not a function")
} catch (e) {
  println("get_function_decl_string(\"not a function\") throws:", e)
}
Output:
get_function_decl_string(add) = pure (table|userdata|instance|class|null).add(a, b: int): any
get_function_decl_string([].insert) = array.insert(arg1: number, arg2): any
get_function_decl_string("not a function") throws: parameter 1 of 'get_function_decl_string' has an invalid type 'string' ; expected: 'function|function'

See also

get_function_info_tableReturns meta information about a function as table
docReturns a documentation string for a function, class, or instance
debugmodule index