mirror of
https://github.com/luau-lang/luau.git
synced 2025-05-04 10:33:46 +01:00
RFC: Constrained Generics
This commit is contained in:
parent
fb2f146123
commit
0285afbd73
1 changed files with 40 additions and 0 deletions
40
rfcs/constrained-generics.md
Normal file
40
rfcs/constrained-generics.md
Normal 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.
|
Loading…
Add table
Reference in a new issue