Skip to content

Commit b9bbb6e

Browse files
authored
Mention typecast multiple return discarding and bidirectional subtyping (#44)
1 parent e7dac49 commit b9bbb6e

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

_pages/typecheck.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,13 +613,24 @@ local myTable = {names = {} :: {string}}
613613
table.insert(myTable.names, 42) -- not ok, invalid 'number' to 'string' conversion
614614
```
615615

616-
A typecast itself is also type checked to ensure the conversion is made to a subtype of the expression's type or `any`:
616+
A typecast itself is also type checked to ensure that one of the conversion operands is the subtype of the other or `any`:
617617
```lua
618618
local numericValue = 1
619619
local value = numericValue :: any -- ok, all expressions may be cast to 'any'
620620
local flag = numericValue :: boolean -- not ok, invalid 'number' to 'boolean' conversion
621621
```
622622

623+
When typecasting a variadic or the result of a function with multiple returns, only the first value will be preserved. The rest will be discarded.
624+
```luau
625+
function returnsMultiple(...): (number, number, number)
626+
print(... :: string) -- "x"
627+
return 1, 2, 3
628+
end
629+
630+
print(returnsMultiple("x", "y", "z")) -- 1, 2, 3
631+
print(returnsMultiple("x", "y", "z") :: number) -- 1
632+
```
633+
623634
## Roblox types
624635

625636
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`).

0 commit comments

Comments
 (0)