2021-07-31 21:04:15 +02:00
## 1 - Variables
Variables defined using #= can't be changed (they are "final"), while the ones affected using the normal operator (=) can.
```
2021-07-31 21:30:58 +02:00
helloworld #= "Hello, World!"
2021-07-31 21:04:15 +02:00
```
## 2 - (Anonymous) Functions
You can make an anonymous functions using the following syntax:
```
2022-04-08 10:53:19 +02:00
getHelloWorld: ~String #= {
2021-07-31 21:30:58 +02:00
return "Hello, World!"
2021-07-31 21:04:15 +02:00
}
```
2021-08-05 00:29:00 +02:00
The type (`~String` here, which basically means "an anonymous function that returns a String") can be omitted, see [#3 ](#3---types ).
2021-07-31 21:04:15 +02:00
2022-04-08 10:53:19 +02:00
To define a **named** (and mandatorily final) function, replace `#=` with `>>` :
2021-07-31 21:04:15 +02:00
```
2021-08-05 00:29:00 +02:00
getHelloWorld: String >> {
2021-07-31 21:04:15 +02:00
return "Hello, World!"
}
```
2021-07-31 22:20:56 +02:00
2022-05-26 13:42:52 +02:00
### 2.1 - Function parameters
2021-07-31 22:20:56 +02:00
Parameters can be added to an anonymous function by specifying the types as follows:
```
2022-04-08 10:53:19 +02:00
addIntToString: (int, String)~ #= { a, b;
2021-12-22 20:53:35 +01:00
print_line(b + a)
2021-07-31 22:20:56 +02:00
}
```
Named functions can have parameters ** (with mandatory names)** like in this example:
```
2021-12-26 14:56:06 +01:00
addIntToString(a: int, b: String) => {
2021-12-22 20:53:35 +01:00
print_line(b + a)
2021-07-31 22:20:56 +02:00
}
```
2022-05-26 13:42:52 +02:00
### 2.2 - Function calls
2021-07-31 22:20:56 +02:00
A function can be simply invoked like this if it has no parameters:
```
helloWorld()
```
If a function does have parameters, you can call it with arguments in the right order; if the function is not anonymous, it's also possible to call it by using the names of the corresponding parameters:
```
addNumbers(int a, int b, int c, int d) >> {
2021-12-22 20:53:35 +01:00
print_line(a + b + c + d)
2021-07-31 22:20:56 +02:00
}
2021-07-31 22:36:03 +02:00
addNumbers(1, 2, 3, 4) # Call without names
addNumbers(a = 1, b = 2, c = 3, d = 4) # Call with names
addNumbers(1, c = 3, b = 2, d = 4)
2021-07-31 22:20:56 +02:00
```
2021-12-22 20:53:35 +01:00
Note that print and print_line are the only functions that will get transpiled to their equivalents.
2021-07-31 22:20:56 +02:00
2021-07-31 21:04:15 +02:00
## 3 - Types
Types are *inferred* , which means that specifying types of variables or returned values is optional.
2021-07-31 21:30:58 +02:00
< sup > Note: While primitives types (`String`, `int` , `double` , `boolean` , `float` ) will be transpiled to their equivalents for the target of the transpiler, this is not the case for other types.< sup >
2022-03-22 18:40:39 +01:00
Every variable has a static type by default at compile time; it is possible to make a **non-final** variable dynamic by adding `dyn` /`dynamic` to the attributes or making it the return type:
2021-07-31 21:04:15 +02:00
```
2021-08-05 00:29:00 +02:00
dyn helloWorld = 0 # helloWorld: dyn = 0 would also work
2021-07-31 21:30:58 +02:00
helloWorld = "Hello, World!"
2021-07-31 21:04:15 +02:00
```
2021-08-05 00:29:00 +02:00
The attributes/return types mentioned above can also be used for functions/anonymous functions:
2021-07-31 21:04:15 +02:00
```
2021-07-31 21:30:58 +02:00
helloWorld: ~dynamic => { return 0 }
2021-08-05 00:29:00 +02:00
dyn helloWorld >> { return 0 } # could be written as helloWorld: dyn >> { return 0 }
2021-07-31 22:20:56 +02:00
```
## 4 - Comments
You can add comments to your code; see the following examples showcasing the available syntaxes:
```
# This is a simple comment
// This is another comment
2021-08-05 00:29:00 +02:00
int ** This is a compact comment, you can insert it pretty much anywhere ** a = 1
```
## 5 - Classes
2021-08-05 01:16:35 +02:00
A class can be made with one of the `class` and `structure` keywords, its name, optional constructors (`~()`) where you can add parameters + code that will run when the class is instantiated, and the class body where you can define methods, properties etc:
2021-08-05 00:29:00 +02:00
```
class Program {
hello #= "Hello"
~(str: String): () {
print(", $str!\n")
}
~() {
print(hello)
}
}
2022-05-26 13:42:52 +02:00
program #= Program("World")
2021-07-31 21:04:15 +02:00
```