Skip to content

Basic Syntax

raziman edited this page Feb 19, 2021 · 3 revisions

Anko is interpreted, dynamic typed language. It resembles closely to go with few syntax and semantics differences.

Variable

No distinction between declaring a variable and assigning to a variable.

x = 12

List

List in anko is heterogenous which simply means it can store any data type.

x = [1, "12", { "score": 90.6 }]

// both operation returns a new list
y = [1, 2, 3] + [1, 2] // [1, 2, 3, 1, 2]
z = [1, 2, 3] + 1 // [1, 2, 3, 1]

// to mutate a list you should use += operator
a = [1, 2, 3]
a += 1 // [1, 2, 3, 1]
a += [10, 10] // [1, 2, 3, 1, 10, 10]

Map

Map is similar to go's map. Internally, the underlying type of anko's map is map[interface{}]interface{}

x = { "name": "John Doe", "age": 12 }
xs = keys(x) // ["name" "age"]

Function

Unlike go, function returns the last expression by default.

add = func(x, y) {
    x + y
}

func sayHi(name) {
    println("Hi " + name)
}

func hiAll(name...) {
    println("Hi ", ...name)
}

println(add(10, 10)) // 20

Loop

Anko has same for statement with go except it does not have for range. You can use for in statement to achieve the same thing but it does not provide index of the current iteration.

for x in [1, 2, 3] {
    println(x)
}

for i = 0; i > 10; i++ {
    println(i)  
}

Conditional

Ternary operator is supported to the language.

if x > 10 {
    ...
} else if x < 10 {

} else {

}

x = 10 > 100 ? "hurmm" : "okay"

Module

Module is a way to scope the variables. Any variable declared within module is not visible to the outside including functions.

x = 30

module Sample {
    x = 12

    function printx() {
        println(x)
    }
}

Sample.printx() // 12

For more, you can have a look at the examples

Clone this wiki locally