From 0c30f7e92ea8e2a676eb935b57955ffa71bf644a Mon Sep 17 00:00:00 2001 From: Junseo Yoo Date: Fri, 14 Jun 2024 10:40:44 -0700 Subject: [PATCH] Revided the reasoning for second drawback --- docs/rawget-type-operator.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/docs/rawget-type-operator.md b/docs/rawget-type-operator.md index 4b37006..b1b0adf 100644 --- a/docs/rawget-type-operator.md +++ b/docs/rawget-type-operator.md @@ -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 = rawget(someTy, someProp) @@ -14,7 +14,7 @@ local prop: rawget = 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 -- Error message: Second argument to rawget 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