Module ammcore.defer

Type-safe utilities for working with errors in Lua.

Index

Function

defer()

Defer code execution till the end of the current block.

xpcall()

Version of FIN xpcall for proper type checking.

Function

ammcore.defer.defer(fn: fun(...any), ...: any) defer: ammcore.defer._Defer

Defer code execution till the end of the current block.

Assign result of this function to a local close variable:

do
    local _<close> = defer.defer(function()
        print("I run at the end of the `do` block!")
    end)

    -- Do something else, maybe even throw an error.

end -- When `_` goes out of scope, the function above is executed.
Parameters:
  • fn (fun(...any)) – a function that will be called upon closing the returned value.

  • ... (any) – function parameters.

Returns:

defer (ammcore.defer._Defer) – an opaque object that should be placed to a close variable.

ammcore.defer.xpcall(fn: fun(...any), ...: any) ok: boolean, err: {message: any, trace: string}

Version of FIN xpcall for proper type checking.

Calls the given function with the given parameters. Returns true if function call was successful, false if function call raised an error.

Parameters:
  • fn (fun(...any)) – a function that will be called in protected environment.

  • ... (any) – function parameters.

Returns:
  • ok (boolean) – true if function call was successful.

  • err ({message: any, trace: string}) – an object describing an error; only returned when ok is false.