Skip to content

How to annotate dynamically created functions? #2444

Answered by tomlau10
AMD-NICK asked this question in Q&A
Discussion options

You must be logged in to vote

There seems no 1 line way to add dynamically created function into a class definition. After all luals will not execute the actual forloop 😅 . I would suggest adding each method using @field manually, like the following:

---@alias Router.method fun(self: Router, path: string, ...)
---@class Router
---@field get Router.method
---@field set Router.method
local ROUTER_MT = {}
ROUTER_MT.__index = ROUTER_MT

ROUTER_MT:  --< here will suggest get|set

methods are created from the methods table in several places: router, route and application

In luals we can add annotation to the same class multiple times, so you may add these @field in different places. luals will be able to union them together:

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@AMD-NICK
Comment options

@tomlau10
Comment options

Answer selected by AMD-NICK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants