Skip to content

Allow callback declarations in declaration files #2000

@AbelToy

Description

@AbelToy

The Luau analyzer supports (undocumented) declaration files, which allow developers of Luau bindings to add types and documentation to their APIs. These files are in turn used by the Luau analyzer (and, in turn, by the third party Luau LSP) to analyze luau code.

Many lua game engines, such as defold or love2d, use global functions as callbacks for user scripts.

For instance, in defold, this global function will be called in every frame

function update(self, dt)
end

As a developer of a game that uses Luau for behavior scripts (and mod support), it would be very helpful for development to be able to specify these callback functions, with the following benefits:

  • Autocomplete callback names and signatures
  • Check if the signature is valid
  • Automatically add documentation to the callback function

Currently, declaring the function using a function declaration results in an error indicating that global functions cannot be overridden.

declare function update(self: GameObject, dt: number)

I think the current behavior is correct. Such declarations are for functions that are available globally.

However, I propose the following potential syntaxes for "callback" declarations:

declare callback update(self: GameObject, dt: number)
declare abstract function update(self: GameObject, dt: number)
declare override function update(self: GameObject, dt: number)

I don't have a particular preference for any syntax as long as the feature can be implemented. Additionally, I can volunteer my time in order to add such a feature, but I wanted to check if this would be accepted first before actually sending a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions