From c34b0c7099d015b3740192ccf83d79c85ce24194 Mon Sep 17 00:00:00 2001 From: Junseo Yoo Date: Fri, 2 Aug 2024 20:18:45 -0700 Subject: [PATCH 1/5] add section about type functions (first draft, todo: add links) --- _pages/typecheck.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/_pages/typecheck.md b/_pages/typecheck.md index 6d5a4cd..9d90656 100644 --- a/_pages/typecheck.md +++ b/_pages/typecheck.md @@ -618,6 +618,37 @@ local value = numericValue :: any -- ok, all expressions may be cast local flag = numericValue :: boolean -- not ok, invalid 'number' to 'boolean' conversion ``` +## Type functions (yet to be released as of August 5) + +Type functions refer to functions that operate on types rather than values. They allow types to be manipulated and transformed, enabling the computation of new types based on existing ones. In Luau, type functions can be defined with the following syntax: +```luau +type function f(...) + -- implementation of the type function +end +``` + +To modify types within type functions, we can use the `type` userdata and its methods [documented here](todo-add-link-here). Note that this userdata is *only* available in the context of type functions. An example of a type function is: +```luau +type function makeOptional(tbl) + if not tbl:is("table") then + error("Argument is not a table") + end + + for k, v in tbl:getprops() do + tbl:setprop(k, type.getunion(v, type.niltype)) + end + + return tbl +end + +type Person = {name: string, age: number} +local bob: makeOptional = {name = "Bob"} -- {name: string?, age: number?} +``` + +In this example, the makeOptional type function takes a table type and modifies its properties to be optional by iterating through the properties and creating a union of the original type and the nil type. + +Note: This feature is currently in beta and has several limitations. We are in the first iteration of this feature. For more details, please read the RFC [here](todo-add-link-here). + ## Roblox types Roblox supports a rich set of classes and data types, [documented here](https://developer.roblox.com/en-us/api-reference). All of them are readily available for the type checker to use by their name (e.g. `Part` or `RaycastResult`). From 608925acde86dffd5dc462945ed0038aa5bc89fd Mon Sep 17 00:00:00 2001 From: Junseo Yoo Date: Mon, 5 Aug 2024 13:48:32 -0700 Subject: [PATCH 2/5] change to lua codeblocks --- _pages/typecheck.md | 99 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 97 insertions(+), 2 deletions(-) diff --git a/_pages/typecheck.md b/_pages/typecheck.md index 9d90656..f200967 100644 --- a/_pages/typecheck.md +++ b/_pages/typecheck.md @@ -621,14 +621,14 @@ local flag = numericValue :: boolean -- not ok, invalid 'number' to 'bo ## Type functions (yet to be released as of August 5) Type functions refer to functions that operate on types rather than values. They allow types to be manipulated and transformed, enabling the computation of new types based on existing ones. In Luau, type functions can be defined with the following syntax: -```luau +```lua type function f(...) -- implementation of the type function end ``` To modify types within type functions, we can use the `type` userdata and its methods [documented here](todo-add-link-here). Note that this userdata is *only* available in the context of type functions. An example of a type function is: -```luau +```lua type function makeOptional(tbl) if not tbl:is("table") then error("Argument is not a table") @@ -649,6 +649,101 @@ In this example, the makeOptional type function takes a table type and modifies Note: This feature is currently in beta and has several limitations. We are in the first iteration of this feature. For more details, please read the RFC [here](todo-add-link-here). + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
MethodDescription
__index(table, index)Fires when table[index] is indexed, if table[index] is nil. Can also be set to a table, in which case that table will be indexed.
__newindex(table, index, value)Fires when table[index] tries to be set (table[index] = value), if table[index] is nil. Can also be set to a table, in which case that table will be indexed.
__call(table, ...)Fires when the table is called like a function, ... is the arguments that were passed.
__concat(table, value)Fires when the .. concatenation operator is used on the table.
__unm(table)Fires when the unary – operator is used on the table.
__add(table, value)The + addition operator.
__sub(table, value)The – subtraction operator.
__mul(table, value)The * mulitplication operator.
__div(table, value)The / division operator.
__idiv(table, value)The // floor division operator.
__mod(table, value)The % modulus operator.
__pow(table, value)The ^ exponentiation operator.
__tostring(table)Fired when tostring is called on the table.
__metatableIf present, locks the metatable so getmetatable will return this instead of the metatable and setmetatable will error. Non-function value.
__eq(table, value)The == equal to operator¹
__lt(table, value)The < less than operator¹
__le(table, value)The <= operator¹
__modeUsed in weak tables, declaring whether the keys and/or values of a table are weak. **Note:** References to Roblox instances are never weak. Tables that hold such references will never be garbage collected.
__gc(table)Fired when the table is garbage-collected. **Note:** On Roblox, this metamethod is disabled.
__len(table)Fired when the # length operator is used on the Object.
__iter(table)Used to denote a custom iterator when using generalized iteration.
+ ## Roblox types Roblox supports a rich set of classes and data types, [documented here](https://developer.roblox.com/en-us/api-reference). All of them are readily available for the type checker to use by their name (e.g. `Part` or `RaycastResult`). From f031a5f0ace07c97a269a49175187640719a5c18 Mon Sep 17 00:00:00 2001 From: Junseo Yoo Date: Mon, 5 Aug 2024 16:16:11 -0700 Subject: [PATCH 3/5] still need to add links --- _pages/typecheck.md | 113 ++++---------------------------------------- 1 file changed, 9 insertions(+), 104 deletions(-) diff --git a/_pages/typecheck.md b/_pages/typecheck.md index f200967..561b184 100644 --- a/_pages/typecheck.md +++ b/_pages/typecheck.md @@ -618,7 +618,7 @@ local value = numericValue :: any -- ok, all expressions may be cast local flag = numericValue :: boolean -- not ok, invalid 'number' to 'boolean' conversion ``` -## Type functions (yet to be released as of August 5) +## Type functions (yet to be released) Type functions refer to functions that operate on types rather than values. They allow types to be manipulated and transformed, enabling the computation of new types based on existing ones. In Luau, type functions can be defined with the following syntax: ```lua @@ -627,9 +627,9 @@ type function f(...) end ``` -To modify types within type functions, we can use the `type` userdata and its methods [documented here](todo-add-link-here). Note that this userdata is *only* available in the context of type functions. An example of a type function is: +To modify types within type functions, we can use the `type` userdata and its methods. Note that this userdata is *only* available in the context of type functions. An example of a type function is: ```lua -type function makeOptional(tbl) +type function partial(tbl) if not tbl:is("table") then error("Argument is not a table") end @@ -642,107 +642,12 @@ type function makeOptional(tbl) end type Person = {name: string, age: number} -local bob: makeOptional = {name = "Bob"} -- {name: string?, age: number?} -``` - -In this example, the makeOptional type function takes a table type and modifies its properties to be optional by iterating through the properties and creating a union of the original type and the nil type. - -Note: This feature is currently in beta and has several limitations. We are in the first iteration of this feature. For more details, please read the RFC [here](todo-add-link-here). - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
MethodDescription
__index(table, index)Fires when table[index] is indexed, if table[index] is nil. Can also be set to a table, in which case that table will be indexed.
__newindex(table, index, value)Fires when table[index] tries to be set (table[index] = value), if table[index] is nil. Can also be set to a table, in which case that table will be indexed.
__call(table, ...)Fires when the table is called like a function, ... is the arguments that were passed.
__concat(table, value)Fires when the .. concatenation operator is used on the table.
__unm(table)Fires when the unary – operator is used on the table.
__add(table, value)The + addition operator.
__sub(table, value)The – subtraction operator.
__mul(table, value)The * mulitplication operator.
__div(table, value)The / division operator.
__idiv(table, value)The // floor division operator.
__mod(table, value)The % modulus operator.
__pow(table, value)The ^ exponentiation operator.
__tostring(table)Fired when tostring is called on the table.
__metatableIf present, locks the metatable so getmetatable will return this instead of the metatable and setmetatable will error. Non-function value.
__eq(table, value)The == equal to operator¹
__lt(table, value)The < less than operator¹
__le(table, value)The <= operator¹
__modeUsed in weak tables, declaring whether the keys and/or values of a table are weak. **Note:** References to Roblox instances are never weak. Tables that hold such references will never be garbage collected.
__gc(table)Fired when the table is garbage-collected. **Note:** On Roblox, this metamethod is disabled.
__len(table)Fired when the # length operator is used on the Object.
__iter(table)Used to denote a custom iterator when using generalized iteration.
+local bob: partial = {name = "Bob"} -- {name: string?, age: number?} +``` + +In this example, the `partial` type function takes a table type and iterates through the properties and modifies them to be optional. + +Note: This feature is currently in beta and has several limitations. We are in the first iteration of this feature. For more details about the `type` userdata and the state of type functions, please read the RFC [here](todo-add-link-here). ## Roblox types From e6ac2f9750c0734d769788a0b2884e8c3c11a8ed Mon Sep 17 00:00:00 2001 From: Junseo Yoo Date: Wed, 7 Aug 2024 12:31:19 -0700 Subject: [PATCH 4/5] add link to todo --- _pages/typecheck.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_pages/typecheck.md b/_pages/typecheck.md index 561b184..bf8d22a 100644 --- a/_pages/typecheck.md +++ b/_pages/typecheck.md @@ -620,7 +620,7 @@ local flag = numericValue :: boolean -- not ok, invalid 'number' to 'bo ## Type functions (yet to be released) -Type functions refer to functions that operate on types rather than values. They allow types to be manipulated and transformed, enabling the computation of new types based on existing ones. In Luau, type functions can be defined with the following syntax: +Type functions refer to functions that operate on types rather than values. They allow types to be manipulated and transformed, enabling the computation of new types based on existing ones. Type functions can be defined with the following syntax: ```lua type function f(...) -- implementation of the type function @@ -647,7 +647,7 @@ local bob: partial = {name = "Bob"} -- {name: string?, age: number?} In this example, the `partial` type function takes a table type and iterates through the properties and modifies them to be optional. -Note: This feature is currently in beta and has several limitations. We are in the first iteration of this feature. For more details about the `type` userdata and the state of type functions, please read the RFC [here](todo-add-link-here). +This feature is currently in beta and has several limitations. We are in the first iteration of this feature. For more details about the `type` userdata and the state of type functions, please read the RFC [here](https://rfcs.luau-lang.org/user-defined-type-functions.html). ## Roblox types From 27d5dada9e15108211042e7db57c02fc33b066b2 Mon Sep 17 00:00:00 2001 From: Junseo Yoo <59751754+joonyoo181@users.noreply.github.com> Date: Thu, 8 Aug 2024 18:52:02 -0700 Subject: [PATCH 5/5] Update typecheck.md --- _pages/typecheck.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_pages/typecheck.md b/_pages/typecheck.md index bf8d22a..33914a8 100644 --- a/_pages/typecheck.md +++ b/_pages/typecheck.md @@ -620,7 +620,7 @@ local flag = numericValue :: boolean -- not ok, invalid 'number' to 'bo ## Type functions (yet to be released) -Type functions refer to functions that operate on types rather than values. They allow types to be manipulated and transformed, enabling the computation of new types based on existing ones. Type functions can be defined with the following syntax: +Type functions refer to functions that operate on types rather than values. They allow types to be manipulated and transformed, enabling new types to be created from existing ones. Type functions can be defined with the following syntax: ```lua type function f(...) -- implementation of the type function @@ -647,7 +647,7 @@ local bob: partial = {name = "Bob"} -- {name: string?, age: number?} In this example, the `partial` type function takes a table type and iterates through the properties and modifies them to be optional. -This feature is currently in beta and has several limitations. We are in the first iteration of this feature. For more details about the `type` userdata and the state of type functions, please read the RFC [here](https://rfcs.luau-lang.org/user-defined-type-functions.html). +This feature will be in beta and have several limitations. We are in the first iteration of this feature. For more details about the `type` userdata and the state of type functions, please read the RFC [here](https://rfcs.luau-lang.org/user-defined-type-functions.html). ## Roblox types