Skip to content

Type safe curry function #846

@Polanas

Description

@Polanas

Greetings! I was wondering if it's possible to implement a simple one argument curry function in a type safe way using new generics capabilities.

---???
local function curry(f, first)
  return function(...)
    return f(first, ...)
  end
end

-- func should evaluate as fun(y: number): number
local func = curry(math.pow)

The first limitation I encountered is incorrect type evaluation when returning an alias from a function, e.g. Parameters.

---@generic F: function
---@param f F
---@return Parameters<F>
function return_params(f) end

--evaluates to `P` instead of (number, number)
local result = return_params(math.pow)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions