Skip to content

How do you capture a generic in a constructor? #616

Closed

Description

@NyakoFox

I'm trying to make a state machine class, where it should capture the state using the arguments.

---@class StateMachine<T : State> : class
local StateMachine = class("StateMachine")

---@generic T : State
---@param state_type T
function StateMachine:init(state_type)
    -- Use it in here
end

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...

For reference, our config:

    "classDefaultCall": {
      "forceNonColon": true,
      "forceReturnSelf": true,
      "functionName": "init"
    },

Activity

  1. xuhuanzy commented on Jul 14, 2025

    @xuhuanzy
    Member

    ---@Generic T : State
    重复声明

  2. NyakoFox commented on Jul 14, 2025

    @NyakoFox
    Author

    Right, sorry, that was a leftover from testing. But after removing that, the problem still occurs.

  3. xuhuanzy commented on Jul 14, 2025

    @xuhuanzy
    Member

    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.

  4. xuhuanzy commented on Jul 14, 2025

    @xuhuanzy
    Member

    Like this?
    Image

  5. NyakoFox commented on Jul 14, 2025

    @NyakoFox
    Author

    Yes, although the problem is that:

    local machine = StateMachine(TestState) is StateMachine<T>, and we want StateMachine<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.

  6. xuhuanzy commented on Jul 14, 2025

    @xuhuanzy
    Member

    Generic support is in its early stages, T: State is currently only used for parameter validation, and you must do it this way if needed.

    Image
  7. xuhuanzy commented on Jul 14, 2025

    @xuhuanzy
    Member

    "forceReturnSelf": true will forcibly convert the return value to self, but self cannot capture generics.

  8. NyakoFox commented on Jul 14, 2025

    @NyakoFox
    Author

    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>

  9. xuhuanzy commented on Jul 14, 2025

    @xuhuanzy
    Member

    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.

  10. added a commit that references this issue on Jul 14, 2025
    21d22e5
  11. xuhuanzy commented on Jul 14, 2025

    @xuhuanzy
    Member

    Already fixed

  12. xuhuanzy commented on Jul 14, 2025

    @xuhuanzy
    Member
    Image
  13. xuhuanzy commented on Jul 14, 2025

    @xuhuanzy
    Member

    This fix only works for returning self; returning StateMachine<T> requires some consideration.

  14. added a commit that references this issue on Jul 14, 2025
    ce46772
  15. 5 remaining items

  16. linked a pull request that will close this issueupdate based on flow #648on Jul 23, 2025
  17. added a commit that references this issue on Jul 24, 2025
    23c1b6e
  18. NyakoFox commented on Aug 3, 2025

    @NyakoFox
    Author

    Could this be reopened, as the behavior was changed but not entirely resolved?

    local engine_state = StateMachine(EngineState)

    engine_state is now a StateMachine<State> rather than a StateMachine<EngineState>.

  19. NyakoFox commented on Aug 3, 2025

    @NyakoFox
    Author
  20. NyakoFox commented on Aug 9, 2025

    @NyakoFox
    Author

    @CppCXY did you see this? Can this issue be reopened as the issue hasn't been resolved (only behavior changed)?

  21. xuhuanzy commented on Aug 9, 2025

    @xuhuanzy
    Member

    Complete, reproducible single-file example. The file must contain all the code; I don't want to guess what those undefined classes are.

  22. NyakoFox commented on Aug 9, 2025

    @NyakoFox
    Author
    --- This is the generic "base" type.
    ---@class BaseClass
    
    --- This extends BaseClass.
    ---@class ExtendedClass : BaseClass
    
    --- This class is a generic, which takes in BaseClass.
    ---@class GenericClass<T : BaseClass>
    local GenericClass = {}
    
    --- This is our constructor, which should take in the base type.
    ---@param state_type T
    function GenericClass:init(state_type)
    end
    
    local return_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>.
  23. self-assigned this
    on Jun 19, 2026
  24. added
    bug
    on Jun 19, 2026
  25. linked a pull request that will close this issueupdate #1113on Jun 19, 2026
  26. added 2 commits that reference this issue on Jun 19, 2026
    df47cd5
    8cbd11d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions