I think it’s tricky to explain what I mean using plain old English, so I will try my best to explain it in Lua.
--!strict
local function variadic_type_parameters<T...>(...: T...): T...
-- ...
end
local a: string, b: number, c: string = variadic_type_parameters("foo", 42, "bar")
-- ^------- ^------- ^------- These are all automatically inferred by the compiler!
type Wrap<T> = {
inner: T
}
local function wrap<T>(inner: T): Wrap<T>
-- ...
end
local function constrained_variadic_type_parameters<T...>(...: Wrap<T...>): T...
-- ... ^--------- This is invalid syntax.
end
local a: unknown, b: unknown, c: unknown = constrained_variadic_type_parameters(wrap("foo"), wrap(42), wrap("bar"))
-- ^-------- ^-------- ^-------- I haven't found a way to make the compiler infer these.
Is there a way to make constrained_variadic_type_parameters
work like variadic_type_parameters
?
As far as I know, this is the closest you can get:
local function type_parameters<A, B, C>(a: A?, b: B?, c: C?): (A, B, C)
-- ... ^-------- You can imagine a bunch of these.
end
local a: string, b: number, c: string = type_parameters("foo", 42, "bar")
-- ^------- ^------- ^------- These are all automatically inferred by the compiler!
local function constrained_type_parameters<A, B, C>(a: Wrap<A>?, b: Wrap<B>?, c: Wrap<C>?): (A, B, C)
-- ... ^-------- You can imagine a bunch of these.
end
local a: string, b: number, c: string = constrained_type_parameters(wrap("foo"), wrap(42), wrap("bar"))
-- ^------- ^------- ^------- These are all automatically inferred by the compiler!
But that is problematic because it breaks if we go over, in this case, three arguments.