-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add section about user-defined type functions #29
base: master
Are you sure you want to change the base?
Changes from all commits
c34b0c7
608925a
f031a5f
e6ac2f9
27d5dad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
||
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 | ||
end | ||
``` | ||
|
||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We now have |
||
```lua | ||
type function partial(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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't match current RFC/implementation (types.getunion, types.singleton(nil)) |
||
end | ||
|
||
return tbl | ||
end | ||
|
||
type Person = {name: string, age: number} | ||
local bob: partial<Person> = {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 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). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be best to drop the beta. Instead of referring to RFC, I would prefer the documentation on our website to document the feature directly, full API included. |
||
|
||
## 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`). | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.