Yerbacon/docs/gettingstarted.md

93 lines
3.0 KiB
Markdown
Raw Normal View History

## 1 - Variables
Variables defined using #= can't be changed (they are "final"), while the ones affected using the normal operator (=) can.
```
helloworld #= "Hello, World!"
```
## 2 - (Anonymous) Functions
You can make an anonymous functions using the following syntax:
```
getHelloWorld: ~String #= {
return "Hello, World!"
}
```
The type (`~String` here, which basically means "an anonymous function that returns a String") can be omitted, see [#3](#3---types).
To define a **named** (and mandatorily final) function, replace `#=` with `>>`:
```
getHelloWorld: String >> {
return "Hello, World!"
}
```
### 2.1 - Function parameters
Parameters can be added to an anonymous function by specifying the types as follows:
```
addIntToString: (int, String)~ #= { a, b;
print_line(b + a)
}
```
Named functions can have parameters **(with mandatory names)** like in this example:
```
addIntToString(a: int, b: String) => {
print_line(b + a)
}
```
### 2.2 - Function calls
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) >> {
print_line(a + b + c + d)
}
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)
```
Note that print and print_line are the only functions that will get transpiled to their equivalents.
## 3 - Types
Types are *inferred*, which means that specifying types of variables or returned values is optional.
<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>
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:
```
dyn helloWorld = 0 # helloWorld: dyn = 0 would also work
helloWorld = "Hello, World!"
```
The attributes/return types mentioned above can also be used for functions/anonymous functions:
```
helloWorld: ~dynamic => { return 0 }
dyn helloWorld >> { return 0 } # could be written as helloWorld: dyn >> { return 0 }
```
## 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
int ** This is a compact comment, you can insert it pretty much anywhere ** a = 1
```
## 5 - Classes
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:
```
class Program {
hello #= "Hello"
~(str: String): () {
print(", $str!\n")
}
~() {
print(hello)
}
}
program #= Program("World")
```