Skip to content

Table of tables of functions #1332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
muppet9010 opened this issue Jul 14, 2022 Discussed in #1331 · 3 comments
Closed

Table of tables of functions #1332

muppet9010 opened this issue Jul 14, 2022 Discussed in #1331 · 3 comments
Labels
enhancement New feature or request
Milestone

Comments

@muppet9010
Copy link

As requested creating an issue for my question on how to approach this situation. Guessing there is no way currently given I was asked to create an issue :)

Discussed in #1331

Originally posted by muppet9010 July 14, 2022
I have the below concept and aren't sure how to go about properly type defining it. I have it as below and the typing almost works fine, apart from within the inner functions the parameters aren't getting their type.

---@alias EntityTypes table<string, EntityTypeDetails>

---@class EntityTypeDetails
---@field GetEntityName fun(name: string, count: integer): string

---@type EntityTypes
local EntityTypeDetails = {
    tree = {
        GetEntityName = function(name, count)
            return name .. tostring(count)
        end
    },
    rock = {
        GetEntityName = function(name, count)
            return name .. tostring(count + 1)
        end
    }
}

local x = EntityTypeDetails["tree"].GetEntityName("grey", 5)

So the function knows its types:
image

But the parameter doesn't:
image

@sumneko sumneko added the enhancement New feature or request label Jul 14, 2022
@max397574
Copy link
Contributor

Screenshot 2022-07-19 at 11 51 25
Here I expected completions (perhaps I have also messed up something)
to me it looks like this is related to this issue
if it isn't tell me so I can open a separate issue

@sondr3
Copy link

sondr3 commented Jul 21, 2022

I've also run into this problem in a task running library I'm writing:

local M = {}

---@class Task
---@field name string
---@field description? string
---@field requires? string[]
---@field enabled? boolean
---@field run fun(ctx: Runtime, data: table): nil

-- Define a task
---@param task Task
---@return Task
M.task = function(task) end

return M

When I'm defining the function itself the ctx parameter loses it types:
image

But when I look at the run field itself, the types are correct:
image

At the moment I'm working around it by adding types, but it seems redundant as you can see that the types are there, they are just lost when creating the function.

M.task({
  name = "task"
  ---@param ctx Runtime
  run = function (ctx)
    local user = ctx:user
  end
})

@muppet9010
Copy link
Author

If it makes it easier to implement then rather than doing inheritance I don't see any large downsides in doing it with the @type applied to the function at creation or a new @interface which is bespoke for this situation. So something that would work like this:

---@alias GetEntityNameInterface fun(name: string, count: integer): string

---@alias EntityTypes table<string, EntityTypeDetails>

---@class EntityTypeDetails
---@field GetEntityName GetEntityNameInterface 

---@type EntityTypes
local EntityTypeDetails = {
    tree = {
        ---@interface GetEntityNameInterface 
        GetEntityName = function(name, count)
            return name .. tostring(count)
        end
    },
    rock = {
        ---@interface GetEntityNameInterface
        GetEntityName = function(name, count)
            return name .. tostring(count + 1)
        end
    }
}

local x = EntityTypeDetails["tree"].GetEntityName("grey", 5)
}

@sumneko sumneko added this to the 3.6.0 milestone Sep 19, 2022
@sumneko sumneko closed this as completed in 3bf48c9 Nov 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants