Types are first-class citizens

This commit is contained in:
2022-08-22 01:59:29 +01:00
parent 9c995830e2
commit df6bd58b1a
10 changed files with 76 additions and 26 deletions

View File

@@ -87,6 +87,7 @@ Variable names in Toy may have a type. These types are:
* dictionary - a collection of indexable key-value pairs
* function - chunks of reusable code
* any - any of the above
* type - the type of types
Types are optional attachments to names - they can be specified as such:
@@ -102,13 +103,21 @@ Types are not interoperable, but in some cases they can be converted from one ty
var y : float = (float)x;
```
defining the type of a variable is not required - in such a case, the type is "any".
Defining the type of a variable is not required - in such a case, the type is "any".
```
//define the variable named "v" with any type
var z = 1;
```
Variables are first-class citizens, meaning they can be stored in and used from variables:
```
//this was originally unintended, but interesting
var t: type = int;
var u: t = 42;
```
## Const
The "const" keyword can be appended to the type of a variable to fix the value in place - constants, as they're known, can't be changed once they are given a value at declaration.