Basic Syntax
Comments
Dyn has two types of comments: line comment and block comment.
Line comment starts with //.
// this is line comment// this is line commentBlock comment starts with /* and ends with */.
/* this is block comment *//* this is block comment */Interestingly, block comments can be nested!
/*
outer comment
/*
nested block comments are allowed!
*/
*//*
outer comment
/*
nested block comments are allowed!
*/
*/Nil
nil means nothing. nil is like null or None in other languages, representing an empty value.
nil
> nilnil
> nilBooleans
You can represent true value with true and false value with false.
true
> truetrue
> truefalse
> falsefalse
> falseYou can use ! to negate a boolean value.
!true
> false!true
> falseThere are also && and || operators for boolean logic, representing and and or respectively.
true && false
> false
false || true
> truetrue && false
> false
false || true
> trueIntegers
INFO
Currently, Dyn only supports integers. Floating point support will be coming soon.
123
> 123123
> 123As always, you can add two integers together.
1 + 2
> 31 + 2
> 3Try other arithmetic operators like -, *, / and %!
5 - 10
> -5
3 * 4
> 12
24 / 6
> 4
36 % 10
> 35 - 10
> -5
3 * 4
> 12
24 / 6
> 4
36 % 10
> 3String
Both quotes are allowed.
'string'
"string"'string'
"string"You can insert an expression in #{} inside a double-quoted string to make template string.
let age = 18
"I am #{age} years old!"
> "I am 18 years old!"
let a = 3
let b = 5
"#{a} times #{b} is #{a * b}"
> "3 times 5 is 15"let age = 18
"I am #{age} years old!"
> "I am 18 years old!"
let a = 3
let b = 5
"#{a} times #{b} is #{a * b}"
> "3 times 5 is 15"Binding
You can declare variables with let keyword.
let a = 1
> 1let a = 1
> 1let is immutable by default.
let a = 1
a = 2 // error
a += 10 // errorlet a = 1
a = 2 // error
a += 10 // errorYou can make it mutable by explicitly add ! mark.
let! a = 1
a = 2
> 2
a += 10
> 12let! a = 1
a = 2
> 2
a += 10
> 12Block
You can group multiple expressions into a block using {}.
{
let a = 1
let b = 2
let c = a + b
}{
let a = 1
let b = 2
let c = a + b
}The block is also an expression, and it evaluates to the last expression in the block.
let c = {
let a = 1
let b = 2
a + b
}
> 3let c = {
let a = 1
let b = 2
a + b
}
> 3Control flows
If
If expression is used to branch the program based on a condition.
if x > 10 {
print("x is greater than 10!")
}if x > 10 {
print("x is greater than 10!")
}If-else
if x > 10 {
print("x is greater than 10!")
} else {
print("x is less than or equal to 10!")
}if x > 10 {
print("x is greater than 10!")
} else {
print("x is less than or equal to 10!")
}