-
Notifications
You must be signed in to change notification settings - Fork 92
Integral number singletons #203
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
Open
PhoenixWhitefire
wants to merge
5
commits into
luau-lang:master
Choose a base branch
from
PhoenixWhitefire:integral-number-singletons
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+115
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2a89c1e
Propose integral number singletons
PhoenixWhitefire 3879e1e
Add extra note to Motivation about tables with restricted indices
PhoenixWhitefire 8170f4a
Merge branch 'luau-lang:master' into integral-number-singletons
PhoenixWhitefire 28ed790
Heterogeneous arrays and minor edits
PhoenixWhitefire 66af01b
`{ T, U }` syntactic sugar for heterogeneous array types
PhoenixWhitefire File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
|
|
||
| # Integral `number` singletons and heterogeneous arrays | ||
|
|
||
| ## Summary | ||
| Allow for singletons of the `number` primitive, similar to `boolean` (`true`/`false`). | ||
|
|
||
| Allow for them to be used like table properties to form heterogeneous arrays. | ||
|
|
||
| ## Motivation | ||
| Programmers may want to use numeric constants as enumerations. Consider: | ||
|
|
||
| ```luau | ||
| type Deserialized = { | ||
| Items: { integer } | ||
| } | ||
|
|
||
| local function convertFromV1ToV2(_: { string }): { integer } | ||
| -- ... | ||
| end | ||
|
|
||
| local function deserialize(Serialized): Deserialized | ||
| local deserialized = {} | ||
|
|
||
| if Serialized.Version == 1 then | ||
| deserialized.Items = convertFromV1ToV2(Serialized.Items) | ||
| elseif Serialized.Version == 2 then | ||
| deserialized.Items = Serialized.Items | ||
| else | ||
| error("unexpected version") | ||
| end | ||
|
|
||
| return deserialized | ||
| end | ||
| ``` | ||
|
|
||
| As it stands, this code cannot be easily typechecked as the type of `Serialized` would have to be narrowed based on the numeric singleton `.Version`. | ||
| The desired type of `Serialized` would be: | ||
|
|
||
| ```luau | ||
| type SerializedStructure = { | ||
| Version: 1, | ||
| Items: { string } | ||
| } | { | ||
| Version: 2, | ||
| Items: { integer } | ||
| } | ||
| ``` | ||
|
|
||
| , however, this is currently inexpressible. `Version` would have to be left annotated as `number`, which prevents `deserialize` from typechecking | ||
| as the union of `Items` is not narrowed from `{ string } | { integer }`. | ||
|
|
||
| Certain runtimes may expose platform-level APIs that use enumerations that are represented as `number`s. | ||
|
|
||
| Programmers may want to annotate that a table only has a certain limited set of valid number indices. | ||
|
|
||
| They may want to annotate that elements at different indices of a table have different types, opting for arrays instead of dictionaries for performance. | ||
|
|
||
| ## Design | ||
|
|
||
| Integral numeric literals valid in the value language (`0`, `0xF2`, `0b11001`, etc) will be made valid for use in the type language, similar to literals | ||
| such as `nil` or string literals like `"hello"`. They will be inferred as either their singleton or their supertype (`number`) with the same rules as | ||
| string singletons. | ||
| Literals with a decimal component are disallowed and raise a syntax error. | ||
|
|
||
| Unions of the singletons will be narrowable through refinement similar to strings: | ||
|
|
||
| ```luau | ||
| local x: 1 | 0b10 | 0x3 = getX() | ||
|
|
||
| if x == 1 or x == 2 then | ||
| local y: 1 | 2 = x -- No type error | ||
| local z: 3 = x -- Type error | ||
| end | ||
| ``` | ||
|
|
||
| They may be used to define a dependent type similar to table properties: | ||
|
|
||
| ```luau | ||
| type T = { | ||
| [1]: string, | ||
| [2]: vector | ||
| } | ||
|
|
||
| local x: T = { "hello", vector.zero } | ||
| local y = x[1] --> string | ||
| local z = x[2] --> vector | ||
|
|
||
| local a: 1 | 2 = getIndex() | ||
| local w = x[a] --> string | vector | ||
| ``` | ||
|
|
||
| They intersect like table properties: | ||
|
|
||
| ```luau | ||
| type T = { | ||
| [1]: boolean | ||
| } | ||
|
|
||
| type U = { | ||
| [2]: buffer | ||
| } | ||
|
|
||
| type V = T & U --> { [1]: boolean, [2]: buffer } | ||
| ``` | ||
|
|
||
| As syntactic sugar, `{ T, U }` as a table type will be evaluated as `{ [1]: T, [2]: U }`. | ||
|
|
||
| ## Drawbacks | ||
| * Increases complexity of the type system | ||
|
|
||
| ## Alternatives | ||
| * Allow for decimals | ||
| * Singletons for `integer`s | ||
| * Intervals/bounds | ||
| * A type for NaN | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.