Dose anyone know of a way to restrict the possible types for a Generic? An example of this would be:
function foo<T: Vector2 | Vector3>(mainPoint: T, points: {T}): T
...
end
If not i would be fine to just do this instead:
type Vector = Vector2 | Vector3
function foo(mainPoint: Vector, points: {Vector}): Vector
...
end
Uhsleep91
(Uhsleep91)
February 17, 2025, 4:49am
#2
As far as I know, Luau currently does not support generic constraints. There are a couple RFCs for them though, and they do plan on implementing them. I don’t think there’s an exact timeline for it though
luau-lang:master
← Kiiyoko:master
opened 04:24AM - 13 Dec 24 UTC
Hi! This is an RFC to implement extending generics/constraining generics, like i… n other languages. This is meant to build upon #50's weakpoints.
[Rendered](https://github.com/Kiiyoko/luau-rfcs/blob/master/docs/generic-constraints.md)
luau-lang:master
← SecondNewtonLaw:master
opened 03:10AM - 23 Jul 24 UTC
This RFC is a possible implementation on what could be an improvement to the cur… rent generics system, by allowing generic types to be constrained, just as in any other language's implementation of generics, which would allow for generics to be used in other seemingly unrelated places, like more efficient native code generation.
[rendered](https://github.com/SecondNewtonLaw/rfcs/blob/master/docs/type-constraints-on-generic-parameters.md)
1 Like