Basic

1. variable

let i: num = 1
let mut j: num = 1
i = 2 // Error! You can't rebinding a let var.
j = 3 // Fine, it's work.

As you see, let create a immutable reference.

Keyword mut let it became mutable.

2. Built-in Type

These type built-in elz.

  • byte -> i32 in system(but if we can use i8 or others for performance, it can be)
  • bool , true or false -> type: bool
  • nil , nil has a only value, nil -> type: nil
  • str -> [ byte ] -> list<byte>, the value of byte is mapping to Unicode
  • num -> machine's float64(I will manage it automatic at future)
  • list -> [ T ] -> type: list<T>(The implementation of it is not have standard, but their all have to behavior in the same way)
  • map -> [ K: V ] -> type: map<K, V>
  • object -> ( [ T ] ) -> type: object<list<T>>, nightmare
  • fn -> fn<object<...params type>>, nightmare
  • type -> type: type

3. Function

Define

fn add(lv: num, rv: num) -> num {
    return lv + rv
}

The return type -> num is required. If you need to choose type.

Use generic

fn concat<T>(lv: T, rv: T) -> T { # Not yet
    return lv ++ rv
}

But you will find, when T doesn't have ++ operator, this function will cause an error.

Like this:

let res = concat(1, 3) // Oh! Compile time error, num doesn't have ++ operator

It's a good news. Only when you used it, it fail.

Why? Because if you checking the type inside the function, then you will need to write a lot of code to help compiler compile it.

Like what Swift do. But, compiler should be designed to help you, not give you more job.

So, how to let it work when type doesn't have something?

Overload will help you.

fn concat(lv: num, rv: num) -> num {
    // ...
}

results matching ""

    No results matching ""