Taly docs changes about

1. Getting started

1.1. Installation

Work in progress!

1.2. Hello world

hello_world.taly

fn main() => println("Hello, world!")
                

The main main is the start of the program. Here we are calling the println function (from the std library which is included by default), with the string "Hello, world".

2. Basics

2.1. Comments

comments.taly

# This is a comment!
                

In Taly, comments starts with a # symbol. There is no multi-line comment.

2.2. Function

Here is the generic syntax to define a function:

function_syntax.taly

fn name(arg0: Type0, arg1: Type1, ...): ReturnType => # body
                

Let's now look at an example:

function_example1.taly

fn add(lhs: I32, rhs: I32): I32 => lhs + rhs
                

This add function takes two parameter: an I32 named lhs and another I32 named rhs, it returns an I32. Essentially this function computes the sum of two I32.

Let's now look at a more complicated example, the factorial function:

factorial.taly

fn factorial(n: U8): U64 =>
    if n <= 1 then 1
    else factorial(n - 1) end
                

You can see in this function the use of an if statement, we will discuss that in Section 2.7.

2.2.1. Early return

The return keyword can be used to return earlier out of a function. We could rewrite the factorial function like this:

factorial_early_return.taly

fn factorial(n: U8): U64 =>
    if n <= 1 then
        return 1
    end 
    factorial(n - 1)
                

2.2.2. Default parameters

It is also possible to define parameters with default value.

default_parameter.taly

fn factorial(n: U8 = 1): U64 => # body
                

This would allow us to call the factorial function without giving it an argument. In this case the default value will be used.

default_parameter_call1.taly

factorial()
                

That is the same as this:

default_parameter_call2.taly

factorial(1)
                

Be careful: Default parameters must be defined last!

2.2.3. Parameter ordering

It is possible to pass parameters in a different order than the one defined:

parameter_ordering.taly

fn subI32(lhs: I32, rhs: I32): I32 => lhs - rhs

subI32(1, 2) # 1 - 2 = -1
subI32(rhs: 1, lhs: 2) # 2 - 1 = 1
subI32(rhs: 1, 2) # The last parameter can be inferred
                

2.3. Variables

There are two ways of storing data:
 - the first one is storing in into a constant.
 - the second one is storing in into a variable.
Constants are immutable, that means that their values cannot be changed.

parameter_ordering.taly

const PI: F32 = 3.1415
var x = PI + 2
                

You can also see for the second variables no types have been specified, this is because the compiler is smart enough to determine which type it should be.

2.4. Data types

Type Description
I8 Signed 8-bit integer
I16 Signed 16-bit integer
I32 Signed 32-bit integer
I64 Signed 64-bit integer
ISize Signed arch integer
U8 Unsigned 8-bit integer
U16 Unsigned 16-bit integer
U32 Unsigned 32-bit integer
U64 Unsigned 64-bit integer
USize Unsigned arch integer
Bool Boolean (true or false)
Char Character (e.g. 'a')
String Boolean (e.g. "Hello")

2.4.1. Option

options.taly

var my_opt: Option[I8] = None
my_opt = 2
                

Options can either be None or contains a value (in this case an I8). This allows you to have variables with no value, but we will discuss more about those later.

2.4.2. Array

options.taly

const my_array: Array[I8] = [0, 1, 2]
                

Arrays are a datatype that can contains multiple data of the same type, however they cannot be extended nor shrank.

2.4.3. Pointer

pointers.taly

var x: I32 = 10
const x_ptr: Pointer[I32] = x.pointer()
                

Pointers are mainly used to communicate with a library in c. The pointer function is defined for every type (even custom type).

2.4.4. Tuple

tuples.taly

var my_tuple: (String, I32) = ("Hello", 10)
var long_tuple: (F32, String, I32) = (9.3, "Hello", 10)
                

Tuples are datatypes that can contains multiple data of different types.

2.5. Type definition

type_def.taly

type StringI32 = (String, I32)

fn main() =>
    const foo: StringI32 = ("hello", 16)
                

2.6. Operators

2.6.1. Algebraic

Operator description
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder


Here is an example on how to use them:

algebraic.taly

const a = 3 + 4
const b = 12 - 4
const c = 6 * 2
const d = 6 / 2
const e = 7 % 2
                

2.6.2. Boolean

Operator description
and Boolean AND
or Boolean OR
xor Boolean XOR
not Boolean Not (Unary)


Here is an example on how to use them:

algebraic.taly

const a = true and false
const b = true or false
const c = true xor false
const d = not true
                

2.6.3. Comparison

Operator description
== Is equal to
!= Is not equal to
< Is less than
<= Is less or equal than
> Is greater than
>= Is greater or equal than


Here is an example on how to use them:

algebraic.taly

const a = 5 == 5
const b = 3 != 4
const c = 9 < 12
const c = 2 <= 3
const c = 8 > 7
const c = 1 >= 1
                

2.6.4. Type comparison

Operator description
is Type is
isnt Type is not
extends Type extends


Here is an example on how to use them:

algebraic.taly

const a: I32 = 13
const b = a is I32
const c = a isnt I64
const d = a extends IAdd
                

2.6.5. Precedence

2.6.6. Short assignment

2.7. If statement

if.taly

if condition then
    # body
elif condition then
    # body
else
    # body
end
                

If statements can have has many elif branch as you need but only one else branch.

2.7.1. Return out of if

2.8. While loop

while.taly

while condition do
    # body
end
                

2.8.1. Break

Break statement allow you to exit a loop.

while.taly

var i: I32 = 0
while i < 10 do
    if i == 8 then break end
    i += 1
end
                

In this case the loop will stop once i reaches 8.

2.8.2. Continue

Break statement allow you go to the next iteration (skipping the current one).

while.taly

var i: I32 = 0
while i < 10 do
    if i == 8 then
        i += 1 
        continue 
    end
    # body
    i += 1
end
                

In this case the loop will skip the iteration when i reaches 8.

2.8.3. Labels

2.9. Match statement

3. Data manipulation

3.1. Classes

3.1.1. Access Modifiers

3.1.2. Constructors

3.1.3. Destructors

3.2. Interfaces

3.3. Prototypes

3.4. Inheritance

3.5. Generics

3.5.1. Constraints

3.5.2. Optional implementation

3.6. Spaces

3.7. Enumerations

3.8. Use

3.8.1. Selection

3.8.2. Renaming

3.9. Casting

3.10. Lambda

4. Advanced concepts

4.1. Referencing

4.2. Options

4.3. Arrays

4.3.1. Foreach loop

4.4. Errors

4.4.1. Definition

4.4.2. Try catch

4.5. Extension

4.5.1. Sealed class

4.6. Operator overloading

4.7. Implicit casting

4.8. Any

4.9. Function scope

5. Foreign Function Access

5.1. Standard C

5.2. External library

5.3. Using pointers

6. Idioms

6.1. Naming convention

6.2. Project structure

6.2.1. Configuration file