Documentation of the Mamba Language

View the Project on GitHub JSAbrahams/mamba_doc

🏠 Home

2 🛠 Features

2.2 📝 Data

2.2.2 Defining Variables and Functions (or Methods)

Every definition must be preceded with the def keyword. This is not necessary when reassigning however.

Functions and methods cannot be reassigned, mutable values can however. A value is an expression which may be evaluated.


A variable definition has the following structure:

def [ mut ] <string> <- <expression>

For instance, a variable x is assigned to as such:

def x <- <expression>


def mut x <- <expression>


A function definition has the following structure:

def <string> ( { <expression> [ : <expression> ] } ) [ : <expression> ] => <expression or statement>

So for instance, we can define a function as follows:

def factorial(n: Int): Int =>
    if n = 0 then 1
    else          n * self.factorial (n - 1) 

A few things to note:

We must always include the types of the argument of a function. We may however omit the return type of a function if it is inferrable from the body. The return type can also be ommitted if the function does not return anything. This is effectively the same as saying the function returns None.

Postfix Notation

When a method or function has only one argument, we can use postfix notation when writing calling that function or method:

    my_function 10 # also works fine
    my_object.my_method 10  # this works too
    my_object my_method 10  # also works
    my_object my_method(10) # we can mix and match if we really want

Default values

We can have default values:

class MyClass
    def my_field <- 5
    def my_method(x: Int, y: Int <- 2) => self.my_field <- x + y

We can now call the method as such:

    def my_class <- MyClass()
    my_class.my_method(10, 2) # works fine
    my_class.my_method(10)    # exactly the same arguments as the function call above
    my_class my_method 10     # using postfix notation
    my_class my_method(10)    # also works
    my_ckass.my_method(30, 5) # or just call it with something else

Default behaviour (Language feature ommitted for now, under review)

We can assign default behaviour to a method or function. To demonstrate this, we will use a toy factorial example. You might first write it as such:

def factorial(n: Int): Int =>
    if n = 0 then 1
    else n * self.factorial (n - 1) 

However, we could make this look much better with default behaviour.

def factorial (n: Int): Int => n * self.factorial (n - 1) # for all other values of n, this function is called
def factorial (0): Int      => 1                     # if n is 0, then this function is called instead

As long as a version exists of a function or method with arguments this is allowed.