Skip to content

Conversation

xuhuanzy
Copy link
Member

@xuhuanzy xuhuanzy commented Sep 30, 2025

泛型实现标准

类型T... 的作用是将类型T转换为可变序列, 可变序列允许匹配多参数类型与多返回值

---@generic T
---@param ... T... # 所有传入参数合并为一个`可变序列`, 即(T1, T2, ...)
---@return T # 返回可变序列
function f1(...) end

---@generic T
---@param ... T...
---@return T... # `...`的作用是转换类型为序列, 此时 T 为序列, 那么 T... = T
function f2(...) end

---@generic T
---@param ... T # T为单类型, `@param ... T`在语义上等同于 TS 的 T[]
---@return T # 返回一个单类型
function f3(...) end

---@generic T
---@param ... T # T为单类型
---@return T... # 将单类型转为可变序列返回, 即返回了(T, T, T, ...)
function f4(...) end



---@alias ConstructorParameters<T> T extends new (fun(...: infer P): any) and P or never

---@generic T
---@param name `T`
---@param ... ConstructorParameters<T>... # ConstructorParameters<T> 返回了一个元组, ... 将元组转为`可变序列`
function f(name, ...)
end



---@alias Predicate<A> fun(...: A...): boolean # A 此时为`元组`, `A...`转换`元组`为`可变序列`以匹配`...`

---@type Predicate<[string, integer, table]> # 传入元组
pred = function() end

定义函数调用时泛型

必须要在函数名称与(之间使用@<>标注类型

call_generic--[[@<number | string>]](1, "2")

This comment was marked as resolved.

gemini-code-assist[bot]

This comment was marked as resolved.

@xuhuanzy xuhuanzy force-pushed the Generics branch 2 times, most recently from aac05a6 to 3374750 Compare October 1, 2025 14:49
@xuhuanzy xuhuanzy linked an issue Oct 1, 2025 that may be closed by this pull request
@xuhuanzy xuhuanzy force-pushed the Generics branch 2 times, most recently from 945e7f0 to 4467089 Compare October 4, 2025 22:45
@xuhuanzy xuhuanzy linked an issue Oct 5, 2025 that may be closed by this pull request
@Aetherall
Copy link

Hi @xuhuanzy, sorry to bother, I wondered if this PR was allowing to infer the content of generic types captured in parameter ?

Something along the lines of

---@generic T
---@param future Future<T> 
---@return T
---@async
function await(future)
  return future:wait()
end

--- somewhere in uv coroutine context
local result = await fetchUser() --- Future<User>
---    ^ User

Anyway I am so gratetful to start a lua project in this times where this project make it really close to type systems I am used to working with ! Really a great future for embedded scripting <3

@xuhuanzy
Copy link
Member Author

xuhuanzy commented Oct 5, 2025

@Aetherall This PR is not needed. It already supported capturing generic parameters before this PR. This PR provides advanced usage, such as conditional types, mapped types, etc.

image

@xuhuanzy
Copy link
Member Author

xuhuanzy commented Oct 5, 2025

Awesome ! Won't this alias definition be simplified by the checker though ? will it work with

---@alias Future<T> { wait: fun () -> T }
image

@Aetherall
Copy link

I must be using incorrect version or settings, thanks for the confirmation ! Mapped types will allow me to remove a really big chunk of annotations in my project ahah many thanks for this PR

@Aetherall
Copy link

You're cooking here, I paused my project because this would allow to remove a good third of my lua files. can't wait for this to drop

@xuhuanzy
Copy link
Member Author

xuhuanzy commented Oct 9, 2025

You're cooking here, I paused my project because this would allow to remove a good third of my lua files. can't wait for this to drop

The main functions have been completed, but we need to wait for @CppCXY to conduct a cross-review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants