mirror of
https://github.com/luau-lang/rfcs.git
synced 2025-05-04 10:43:48 +01:00
Revided the reasoning for second drawback
This commit is contained in:
parent
235b241b64
commit
0c30f7e92e
1 changed files with 6 additions and 4 deletions
|
@ -2,11 +2,11 @@
|
|||
|
||||
## Summary
|
||||
|
||||
This RFC proposes the addition on one type operator, `rawget`, which can be used to look up a specific property of another type *without* invoking the `__index` metamethod.
|
||||
This RFC proposes the addition of a new type operator, `rawget`, which can be used to look up a specific property of a table type *without* invoking the `__index` metamethod.
|
||||
|
||||
## Motivation
|
||||
|
||||
Given that `rawget` is a built-in runtime operator in the language ([rawget Lua Globals](https://create.roblox.com/docs/reference/engine/globals/LuaGlobals#rawget)), it feels natural for there to be a version of this as a type operator. As such, the main motivation behind this feature is to close out holes in Luau's type families and allow Luau developers to be more expressive writing typed code:
|
||||
Currently, `rawget` is a built-in runtime operator in the language ([rawget in Luau Global functions](https://luau-lang.org/library#global-functions)) that is missing a corresponding type operator that captures its behavior. This RFC seeks to address this hole in the type system by providing a builtin type operator for `rawget` that will allow Luau developers to express more accurate types in their code:
|
||||
|
||||
```luau
|
||||
local prop: rawget<someTy, someProp> = rawget(someTy, someProp)
|
||||
|
@ -14,7 +14,7 @@ local prop: rawget<someTy, someProp> = rawget(someTy, someProp)
|
|||
|
||||
## Design
|
||||
|
||||
The functionality of `rawget` type operator behaves the same as its [runtime pair](https://create.roblox.com/docs/reference/engine/globals/LuaGlobals#rawget): provide a way to look up a specific property of a type without invoking the `__index` metamethod.
|
||||
The functionality of the `rawget` type operator behaves the same as its [runtime counterpart](https://luau-lang.org/library#global-functions). Namely, it provide a way to look up a specific property of a table type without invoking the `__index` metamethod.
|
||||
|
||||
```luau
|
||||
local var1 = {
|
||||
|
@ -36,13 +36,15 @@ local key = "property"
|
|||
type age = rawget<Person, key> -- Error message: Second argument to rawget<Person, _> is not a valid index type; Unknown type 'key'
|
||||
```
|
||||
|
||||
Note: `rawget` type operator does not work on class types because they do not have direct fields that can be looked up without invoking its metamethod.
|
||||
|
||||
The implementation effort for this type operator is very minimal. Since the `rawget` type operator functions similarly to the `index` type operator, we can reuse the functions already used to implement the `index` type operator.
|
||||
|
||||
## Drawbacks
|
||||
|
||||
There aren't appreciable drawbacks. One possible drawback is the increase in the size of the codebase, which may complicate maintenance. However, this is not expected to be a major drawback since the code for `index` type operators already exists. By reusing the code, it is even fair to say that there will be less than 50 lines of code needed to implement this type operator.
|
||||
|
||||
Another drawback can come from the extra knowledge a user may need in order to use this type operator. For example, users will need to know the inner workings of types and how different operations interact with them (for instance, how `index` interacts with metatables that have the `__index` metamethod). However, there are documentations that outline each interactions, and thus this drawback does not seem to pose an issue.
|
||||
Another drawback can come from the extra knowledge a user may need in order to use this type operator. For example, users will need to know the inner workings of types and how different operations interact with them (for instance, how `index` interacts with metatables that have the `__index` metamethod). However, this additional complexity is manageable because the language already incorporates such complexities at the runtime level. Developers are already familiar with concepts like `rawget`, so this does not add much new complexity.
|
||||
|
||||
## Alternatives
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue