镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Support for Operator type annotations #599

Closed

Description

@Stanzilla

It does not look like Emmy does have native support for Operator type annotations but I was wondering if it was possible to add it to the LSP?

Activity

  1. sumneko commented on Jul 15, 2021

    @sumneko
    Collaborator

    Considering performance issues, a new annotation should be provided for explicit marking.

  2. Stanzilla commented on Jul 15, 2021

    @Stanzilla
    Author

    @sumneko but that is still something you can do inside of the LSP, right?

  3. sumneko commented on Jul 16, 2021

    @sumneko
    Collaborator

    My current assumption is this:

    ---@class A
    ---@operator add string
    
    ---@type A
    local a
    local r = a + 1 -- `r` is `string` here

    For performance reasons, you must define this explicitly, otherwise the cost performance from metatable search is too low.

  4. Benjamin-Dobell commented on Jul 17, 2021

    @Benjamin-Dobell

    May I please instead suggest:

    unary_op ::= 'unm'
    binary_op ::= 'add' | 'sub' | 'mul' | 'div' | 'idiv' | 'mod' | 'pow' | 'concat'
    unary_operator ::= unary_op ':'? ty
    binary_operator ::= binary_op '(' ty ')' ':' ty
    tag_operator ::= 'operator' (unary_operator | binary_operator) comment_string?
    

    In order to cover:

    ---@class A
    ---@operator unm: A
    ---@operator add(A): A
    ---@operator add(number): number
    
    ---@type A
    local a1
    
    ---@type A
    local a2
    
    local foo = a1 + a2 -- A
    local bar = a1 + 1 -- number
    local huzzah = -a1 -- A
  5. sumneko commented on Jul 19, 2021

    @sumneko
    Collaborator

    @Benjamin-Dobell This kind of generic support may bring a relatively large overhead, I don’t want to support it for the time being

  6. Benjamin-Dobell commented on Jul 19, 2021

    @Benjamin-Dobell

    Sorry, just to to clarify, I'm not requesting you add all those operators or that you infer the right operand's type. Instead I'd like to try come up with a syntax that is at least future proof with respect to types, in order to avoid the pitfalls of LDoc.

    I'm the developer of Luanalysis. Luanalysis does not yet have this functionality but, when implemented, will use the right operand's type when inferring the resultant type of the expression. Even if you opt to ignore the right operand's type, it'd be ideal if we supported the same syntax.

  7. sumneko commented on Jul 19, 2021

    @sumneko
    Collaborator

    Sorry, just to to clarify, I'm not requesting you add all those operators. Instead I'd like to try come up with a syntax that is future proof with respect to types, in order to avoid the pitfalls of LDoc.

    I'm the developer of Luanalysis. Luanalysis does not yet have this functionality but, when implemented, will use the right operand's type when inferring the resultant type of the expression. Even if you opt to ignore the right operand's type, it'd be ideal if we supported the same syntax.

    It looks good, and I will follow this format to implement it later (including compatibility and ignoring the type of operation object).

  8. Stanzilla commented on Jun 22, 2022

    @Stanzilla
    Author

    @sumneko any news? :)

  9. sumneko commented on Jun 23, 2022

    @sumneko
    Collaborator

    Will support in 3.5.0

  10. added this to the 3.5.0 milestone on Jun 30, 2022
  11. sumneko commented on Jul 6, 2022

    @sumneko
    Collaborator

    Done.

  12. nathanpage-credo commented on Jul 7, 2022

    @nathanpage-credo

    @sumneko Will this also support extending current classes/types. For example, I've used penlight stringx.format_operator which allows "%d %d" % {2,3}. This shows up as a param-type-mismatch.

  13. sumneko commented on Jul 7, 2022

    @sumneko
    Collaborator

    @sumneko Will this also support extending current classes/types. For example, I've used penlight stringx.format_operator which allows "%d %d" % {2,3}. This shows up as a param-type-mismatch.

    Please open a new issue and tell more details.

  14. BribeFromTheHive commented on Nov 19, 2022

    @BribeFromTheHive

    How do I document an __index metamethod? I have a table which extends an arbitrary number of sub-elements via OriginalTableName.subElementName syntax. I have no idea how to tell the extension that the subElementNames are of type "blahBlahBlah" instead of it defaulting to "unknown".

  15. sumneko commented on Nov 21, 2022

    @sumneko
    Collaborator

    How do I document an __index metamethod? I have a table which extends an arbitrary number of sub-elements via OriginalTableName.subElementName syntax. I have no idea how to tell the extension that the subElementNames are of type "blahBlahBlah" instead of it defaulting to "unknown".

    ---@class Cat: Animal

  16. CelticMinstrel commented on Feb 5, 2023

    @CelticMinstrel

    I have a question about the operator annotation. Is it also required if the operator metafunction is fully declared in the Lua file? Or is that detected separately?

  17. sumneko commented on Feb 6, 2023

    @sumneko
    Collaborator

    I have a question about the operator annotation. Is it also required if the operator metafunction is fully declared in the Lua file? Or is that detected separately?

    Operator annotation is required.

  18. CelticMinstrel commented on Feb 6, 2023

    @CelticMinstrel

    Another question. What about the comparison operators? That is, eq / lt / le. Currently it reports those to be an unknown operator.

  19. sumneko commented on Feb 6, 2023

    @sumneko
    Collaborator

    Another question. What about the comparison operators? That is, eq / lt / le. Currently it reports those to be an unknown operator.

    Not supported yet.
    If you have other questions or feature requests, please open a new issue.

  20. luiz00martins commented on Nov 8, 2024

    @luiz00martins

    How do I document an __index metamethod? I have a table which extends an arbitrary number of sub-elements via OriginalTableName.subElementName syntax. I have no idea how to tell the extension that the subElementNames are of type "blahBlahBlah" instead of it defaulting to "unknown".

    ---@class Cat: Animal

    I'm also trying to type the index operator in my class, but I didn't quite understand what this means. As an example:

    ---@class Example
    ---@operator call: string
    ---@field field fun(): number
    local example = setmetatable({}, {
      __call = function(_, ...)
        return "a"
      end,
      __index = function(_, key)
        return 1
      end,
      field = function() return 2 end,
    })
    
    local a = example.field()
    local b = example()
    local c = example[1]
    

    In the example above, a is correctly deduced as a number, and b is correctly deduced as a string, but c is unknown. Is it possible to make the LSP deduce c to be a number?

  21. BribeFromTheHive commented on Nov 8, 2024

    @BribeFromTheHive

    class Example: {[number]: number}

  22. tomlau10 commented on Nov 8, 2024

    @tomlau10
    Contributor

    one can also use ---@field [KeyType] ValueType to define the index operation type

    ---@class Example
    ---@operator call: string
    ---@field field fun(): number
    ---@field [any] number
    local example = {}
    
    local a = example.field() -- number
    local b = example() -- string
    local c = example[1] -- number
    local d = example['2'] -- number
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions