You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How do you capture a generic in a constructor? #616
I'm trying to make a state machine class, where it should capture the state using the arguments.
---@classStateMachine<T:State> :classlocalStateMachine=class("StateMachine")
---@genericT:State---@paramstate_typeTfunctionStateMachine:init(state_type)
-- Use it in hereend
Unfortunately, just calling local machine = StateMachine(TestState) does not make a StateMachine<TestState>, only a StateMachine. Is there a way I can annotate this properly so it works? Ideally I wouldn't need to ---@type StateMachine<TestState> above the line...
However, while writing this library, I used some features that haven't been merged into the main branch yet, so it's normal for it to report errors in some places.
I understand. Will this be possible in the future? A constructor to capture a generic type should still be possible even though we are using that. Maybe that option can do self, or if your class is Something, it can be somehow told to return self<T>
--- This is the generic "base" type.---@classBaseClass--- This extends BaseClass.---@classExtendedClass:BaseClass--- This class is a generic, which takes in BaseClass.---@classGenericClass<T:BaseClass>localGenericClass= {}
--- This is our constructor, which should take in the base type.---@paramstate_typeTfunctionGenericClass:init(state_type)
endlocalreturn_val=GenericClass({} --[[@as ExtendedClass]])
--- return_val is now a GenericClass<BaseClass>, despite getting an input of ExtendedClass.--- This was improved in #648, as prior it'd just return GenericClass.--- However, the expected output is GenericClass<ExtendedClass>.
Activity
xuhuanzy commentedon Jul 14, 2025
---@Generic T : State
重复声明
NyakoFox commentedon Jul 14, 2025
Right, sorry, that was a leftover from testing. But after removing that, the problem still occurs.
xuhuanzy commentedon Jul 14, 2025
Reference luakit/class.lua
https://github.com/xuhuanzy/rxlua
xuhuanzy commentedon Jul 14, 2025
However, while writing this library, I used some features that haven't been merged into the main branch yet, so it's normal for it to report errors in some places.
xuhuanzy commentedon Jul 14, 2025
Like this?

NyakoFox commentedon Jul 14, 2025
Yes, although the problem is that:
local machine = StateMachine(TestState)isStateMachine<T>, and we wantStateMachine<TestState>.I know you can
---@type StateMachine<TestState>but there must be a way to capture it properly using the argument.I did look inside of behaviorSubject.lua and it looks like it does what we do, so I would not be surprised if it does not work properly either.
xuhuanzy commentedon Jul 14, 2025
Generic support is in its early stages,
T: Stateis currently only used for parameter validation, and you must do it this way if needed.xuhuanzy commentedon Jul 14, 2025
"forceReturnSelf": truewill forcibly convert the return value toself, butselfcannot capture generics.NyakoFox commentedon Jul 14, 2025
I understand. Will this be possible in the future? A constructor to capture a generic type should still be possible even though we are using that. Maybe that option can do
self, or if your class is Something, it can be somehow told to returnself<T>xuhuanzy commentedon Jul 14, 2025
Wait for support, but you must switch to https://github.com/xuhuanzy/emmylua-analyzer-rust/tree/update.
The branch has diverged from the main branch, and it might not be fully merged into the main branch until the version after next.
fix EmmyLuaLs#616
xuhuanzy commentedon Jul 14, 2025
Already fixed
xuhuanzy commentedon Jul 14, 2025
xuhuanzy commentedon Jul 14, 2025
This fix only works for returning
self; returningStateMachine<T>requires some consideration.fix EmmyLuaLs#616
5 remaining items
fix EmmyLuaLs#616
NyakoFox commentedon Aug 3, 2025
Could this be reopened, as the behavior was changed but not entirely resolved?
engine_stateis now aStateMachine<State>rather than aStateMachine<EngineState>.NyakoFox commentedon Aug 3, 2025
( @xuhuanzy @CppCXY )
NyakoFox commentedon Aug 9, 2025
@CppCXY did you see this? Can this issue be reopened as the issue hasn't been resolved (only behavior changed)?
xuhuanzy commentedon Aug 9, 2025
Complete, reproducible single-file example. The file must contain all the code; I don't want to guess what those undefined classes are.
NyakoFox commentedon Aug 9, 2025
fix(generic): infer class generic args for self overload returns
fix(generic): infer class generic args for self overload returns