diff --git a/src/content/wiki/annotations.mdx b/src/content/wiki/annotations.mdx index 4d55632..07381bb 100644 --- a/src/content/wiki/annotations.mdx +++ b/src/content/wiki/annotations.mdx @@ -647,7 +647,7 @@ local Numbers = {} ### @generic -Generics allow code to be reused and serve as a sort of "placeholder" for a type. Surrounding the generic in backticks (`) will capture the string value from the argument and infer the named class/type as the generic type. [Generics are still WIP](https://github.com/LuaLS/lua-language-server/issues/1861). +Generics allow code to be reused and serve as a sort of "placeholder" for a type. Surrounding the generic in backticks (`) will capture the string value from the argument and infer the named class/type as the generic type. [Generics are still WIP](https://github.com/LuaLS/lua-language-server/issues/1861). Since `v3.17.0`, classes can declare type parameters (e.g. `Box`), classes can inherit from an instantiated generic class (e.g. `IntegerBox: Box`), generic type parameters work in [`@overload`](#overload) annotations, and `fun(...)` inline generic function types are supported in [`@field`](#field) and [`@type`](#type) annotations. **Syntax** @@ -733,6 +733,52 @@ dict["foo"] = "bar?" dict["correct"] = true ``` + + +Generic Class with Fields and Methods + +```lua +---@class Box +---@field value T +---@field get fun(self: Box): T + +---@type Box +local box + +-- `v` is an integer here +local v = box.value + +-- `got` is an integer here +local got = box:get() + +---A class can also inherit from an instantiated generic class (v3.17.0+) +---@class IntegerBox: Box + +---@type IntegerBox +local ibox + +-- `v2` and `got2` are integers here as well +local v2 = ibox.value +local got2 = ibox:get() +``` + +Note: for the type parameter to be substituted through inheritance, members must be declared as `---@field` entries in the class annotation block. A method declared separately (e.g. `---@return T` above `function Box:m() end`) only has `T` substituted when the receiver's type is the instantiated generic class itself (e.g. a value of type `Box`), not when the method is reached through an inheriting class like `IntegerBox`. + + + +Inline Generic Function Type (v3.17.0+) + +```lua +---@class Utils +---@field identity fun(value: T): T + +---@type Utils +local utils + +-- `s` is a string here +local s = utils.identity("hello") +``` + ### @meta