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 comment
Block 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
> nil
nil
> nil
Booleans
You can represent true value with true
and false value with false
.
true
> true
true
> true
false
> false
false
> false
You can use !
to negate a boolean value.
!true
> false
!true
> false
There are also &&
and ||
operators for boolean logic, representing and
and or
respectively.
true && false
> false
false || true
> true
true && false
> false
false || true
> true
Integers
INFO
Currently, Dyn only supports integers. Floating point support will be coming soon.
123
> 123
123
> 123
As always, you can add two integers together.
1 + 2
> 3
1 + 2
> 3
Try other arithmetic operators like -
, *
, /
and %
!
5 - 10
> -5
3 * 4
> 12
24 / 6
> 4
36 % 10
> 3
5 - 10
> -5
3 * 4
> 12
24 / 6
> 4
36 % 10
> 3
String
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
> 1
let a = 1
> 1
let
is immutable by default.
let a = 1
a = 2 // error
a += 10 // error
let a = 1
a = 2 // error
a += 10 // error
You can make it mutable by explicitly add !
mark.
let! a = 1
a = 2
> 2
a += 10
> 12
let! a = 1
a = 2
> 2
a += 10
> 12
Block
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
}
> 3
let c = {
let a = 1
let b = 2
a + b
}
> 3
Control 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!")
}