Skip to content

Basic Syntax

Comments

Dyn has two types of comments: line comment and block comment.

Line comment starts with //.

dyn
// this is line comment
// this is line comment

Block comment starts with /* and ends with */.

dyn
/* this is block comment */
/* this is block comment */

Interestingly, block comments can be nested!

dyn
/*
  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.

dyn
nil
> nil
nil
> nil

Booleans

You can represent true value with true and false value with false.

dyn
true
> true
true
> true
dyn
false
> false
false
> false

You can use ! to negate a boolean value.

dyn
!true
> false
!true
> false

There are also && and || operators for boolean logic, representing and and or respectively.

dyn
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.

dyn
123
> 123
123
> 123

As always, you can add two integers together.

dyn
1 + 2
> 3
1 + 2
> 3

Try other arithmetic operators like -, *, / and %!

dyn
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.

dyn
'string'
"string"
'string'
"string"

You can insert an expression in #{} inside a double-quoted string to make template string.

dyn
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.

dyn
let a = 1
> 1
let a = 1
> 1

let is immutable by default.

dyn
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.

dyn
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 {}.

dyn
{
	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.

dyn
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.

dyn
if x > 10 {
	print("x is greater than 10!")
}
if x > 10 {
	print("x is greater than 10!")
}

If-else

dyn
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!")
}