RFC: Constrained Generics

This commit is contained in:
T 'Filtered' C 2022-12-17 01:14:30 +00:00 committed by GitHub
parent fb2f146123
commit 0285afbd73
Signed by: DevComp
GPG key ID: 4AEE18F83AFDEB23

View file

@ -0,0 +1,40 @@
# Constrained Generics
## Summary
Allows you to give a function generic a constrained type.
## Motivation
Right now, when you create a generic function, the generic paramter is assumed to be everything. This creates problems, especially with tables
```lua
local function SomethingWithAGenericTable<T>(tab: T)
for _, v in t do --Cannot call non-function T (T is not a table)
end
end
```
This constraining is mainly for concrete table types and functions, since types like `number`, `string`, and `boolean` are literal types.
## Design
The way to implement this is to have new syntax under the generic's brackets: `<T: typedef>`. This would constrain the type from the generic def and give the typechecker better hints.
```lua
local function SomethingWithAGenericTable<T: {[any]: any}>(tab: T)
for _, v in t do
print(v)
end
end
SomethingWithAGenericTable({meow = 5}) --OK
SomethingWithAGenericTable(5) --not OK
```
## Drawbacks
This adds unusual syntax, as well as adding to the complexity of the language.
## Alternatives
Do nothing, and use the `assert(type(T) == T)` syntax, though this creates extra bytecode.