Skip to content

Generic parameter with parameter of that generic type #2669

Answered by tomlau10
JonSnowbd asked this question in Q&A
Discussion options

You must be logged in to vote

This seems to me impossible 😕 because when you specify a param with a generic type T, you are capturing its type, not enforcing its type. In order to have autocompletion for the overrides object, you must first specify the actual type of it in the function param, not generic type.


To achieve this, what I can come up with is to modify the Make() syntax:

  • Create a MakeFactory(type) => return a closure function that accepts a param of type type
  • And to avoid creating a closure function variable every time, you may also cache it by type
---@class Human
---@field name string
---@field age integer
local Human = {}
Human.name = "Charles"
Human.age = 30


local factories = {}
---@generic T
---@p…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@JonSnowbd
Comment options

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