tl icon indicating copy to clipboard operation
tl copied to clipboard

Multiple-arity generic params

Open Frityet opened this issue 5 months ago • 3 comments

local type Error<TRet, TErr> = {TRet | nil, TErr}
local type Error<TRet> = Error<TRet, string>

local function divide(x: number, y: number): Error<number>
    if y == 0 then return { nil, "Cannot divide by 0" } end
    return x / y 
end

This is how C# implements "varadict" generic params, and can help with a lot of patterns, i.e arbritary params on a func

Frityet avatar Jul 04 '25 06:07 Frityet

Can it just be type ErrorStr<TRet> = Error<TRet, string>?

0komo avatar Jul 25 '25 06:07 0komo

Can it just be type ErrorStr<TRet> = Error<TRet, string>?

yes, but then you do not have any of the upsides of being able to have default type params

Frityet avatar Jul 25 '25 22:07 Frityet

Teal only supports this kind of polymorphism in record fields coming from Lua APIs (i.e. for interoperability), and we don't support default arguments anywhere else.

I don't think the added complexity to the compiler is worth it. If the purpose is just to save a bit of typing, this kind of shorthand would probably be best served with a macro, in the future.

hishamhm avatar Nov 21 '25 16:11 hishamhm