Welcome to the idea of language based around Kotlin and Rust!
Comments in this language use C-like syntax.
// One-liner
function(/* Insides */) // Description of it!
/*
Multiple lines!
*/
There are multiple ways of declaring variables in this language. By default, every variable is immutable, and you need to specify mutability. The syntax for declaring variables is Rust-like, with an optional let keyword.
Example of an immutable variable:
variable: i32 = 2
let variable: i32 = 2 // `let` is optional and not required; it will work the same in the example above
Example of mutable variable:
mut variable: i32 = 2 //you just put `mut` before name!
let mut variable: i32 = 2 // same here but after `let`
There are also constant values, which are strictly immutable and only allow primitive values. They require the const keyword before the name. They are automatically inlined by the compiler.
const CONSTANT_VARIABLE: i32 = 2
Uses rust-like types
- Signed integers:
i8,i16,i32,i64,i128andisize - Unsigned integer:
u8,u16,u32,u64,u128andusize - Floating-point numbers:
f8,f16,f32,f64,f128andfsize - Characters:
char(to create character use''example:'a') - Boolean:
bool(trueorfalse)
Tuples have a Rust-like syntax. They are groups with multiple types of values inside and have a fixed size. Tuples can be destructured. Example way of creating:
tuple: (fsize, u16, bool) = (1.0, 24, true)
Accessing values is different from Rust. Instead of tuple.i (where i is the index/place of the value in the tuple), it uses array-like syntax for accessing elements.
Example way of accessing value:
value: fsize = tuple[0] //returns 1.0 of type fsize
// remember `tuple` variable is (1.0, 24, true)
You can also destructure tuples using a different variable syntax, which destructures the tuple into multiple variables. Example of destruction:
(float: fsize, number: u16, answer: bool) = tuple
float //returns 1.0 with type fsize
number //returns 24 with type u16
answer //returns true with type bool
Arrays have a Kotlin-like syntax. They are groups of the same type and have a fixed size. Example way of creating:
array: char[5] = ['h','e','l','l','o']
The number inside char[5] defines the fixed size of the array, and char defines the type of values in the array. You can access values inside an array in the same way you would in a tuple.
Example of accessing value:
letter: char = array[3] //returns `l` with type char
Lambdas are a replacement for functions in this language. They use a different syntax from other languages. To create a lambda, you use f() (where f is used to differentiate from tuples). By default, they return nothing. Lambdas are executed like C functions. They also support compact one-line execution, similar to Kotlin.
Example use of lambda in variable
lambda: f() = something()
Lambdas can span multiple lines by using {}.
Example use:
lambda: f() = {
something()
something()
}
Lambdas can also have a return type, which is specified after f(), like f(): i32.
Example use:
lambda: f(): i32 = 2 //will return `2`
You can also specify when the lambda should return, if required; otherwise, it will return the last statement in the block. Instead of using return as in other languages, you use <- to indicate a return.
Example use:
lambda: f(): i32 = {
2+2 //won't be returned
<- 4+4 //will return 8 and stop lambda here
5-5 //won't be reached
}
Lambdas also support arguments. Arguments are specified inside () parentheses, similar to other languages' function syntax.
Example use:
lambda: f(x: i32): i32 = x^2 //if executed `lambda(2)` will return `4`
//You can also specify which argument you pass value by using `lambda(x=2)`
Arguments also support default values, as you would see in Kotlin. Example use:
lambda: f(x: i32 = 2): i32 = x^2 //If not passed anything by doing `lambda()` it will return 4, if did for example `lambda(6)` it will return 36.