Skip to content

Commit 1dbd413

Browse files
committed
Update type hinting
1 parent 61f44fa commit 1dbd413

File tree

1 file changed

+69
-7
lines changed

1 file changed

+69
-7
lines changed

docs/New Features/Type Hinting.md

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22
sidebar_position: 1
33
---
44
Type-hinting looks like this:
5-
```pluto showLineNumbers
5+
```pluto
66
local var: string = "hello world"
77
```
88
It works with functions too:
9-
```pluto showLineNumbers
9+
```pluto
1010
local function myfunc(a: string, b: string): number
1111
return tonumber(a) + tonumber(b)
1212
end
1313
1414
print(myfunc(1, "1")) -- This will emit a warning for argument type mismatch.
1515
```
1616
There's no effect on performance. This is implemented entirely during the compilation phase.
17-
### What types can I use?
17+
18+
### Primitive Types
1819
- `string`
1920
- `number`
2021
- `int`
@@ -24,15 +25,76 @@ There's no effect on performance. This is implemented entirely during the compil
2425
- `table`
2526
- `userdata`
2627
- `nil`
27-
- `void` (return type only)
2828
- `any`
2929

30+
### Multiple Return Values
31+
32+
For function return values, there is the `void` hint to indicate 0 return values, as well as the following syntax for 2 or more:
33+
34+
```pluto
35+
local function get_status(): (bool, string)
36+
return true, "OK!"
37+
end
38+
print(get_status())
39+
```
40+
41+
### Union Types
42+
3043
Prefix a `?` to indicate void-able types, e.g. `?string` indicates that it may be absent, nil, or a string.
3144

3245
You can also use `|` to delimit alternatives, e.g. `string|int` indicates that it may be a string or an int.
3346

3447
Putting all of this together, we could also have e.g. `?string|int` to indicate that it may be absent, nil, a string, or an int.
3548

36-
:::info
37-
This is a WIP feature, and it's very difficult to implement in a one-pass compiler. Allow tolerance for missing coverage, and report any bugs.
38-
:::
49+
### Complex Table Types
50+
51+
You can specify type requirements for fields inside of tables like so:
52+
53+
```pluto
54+
local point: { x: number, y: number } = { x = 1, y = 2 }
55+
```
56+
57+
### Named Types
58+
59+
You can use `$type` to assign a name to a type.
60+
61+
```pluto
62+
$type Point = { x: number, y: number }
63+
64+
local p: Point = { x = 1, y = 2 }
65+
```
66+
67+
### Complex Function Types
68+
69+
You can also specify type expectations for the parameters and/or return values of functions.
70+
71+
```pluto
72+
$type LogCallback = function(msg: string): void
73+
74+
function write_env(log: LogCallback)
75+
log(_VERSION)
76+
log(_PVERSION)
77+
end
78+
79+
write_env(print)
80+
```
81+
82+
### `$declare`
83+
84+
The parser can only do typechecking for what it can see, so `$declare` allows you to directly set the type of globals.
85+
86+
```pluto
87+
$declare _VERSION: string
88+
```
89+
```pluto
90+
$declare function tonumber(str: string, base: ?number): number
91+
```
92+
93+
### `$getproptype`
94+
95+
If you want to know what type the parser propagation has detected for a given variable, you can use `$getproptype` to ask it:
96+
```pluto
97+
local x = "hello"
98+
print($getproptype(x)) --> string
99+
```
100+
Note that the return value of this function should not be relied upon.

0 commit comments

Comments
 (0)