Skip to content

Example Code

Andrew Matthews edited this page Feb 15, 2022 · 3 revisions

TODO: This uses an early version of the language syntax. Update the examples

Example Code

Fifth is based syntactically on C#. It is meant to be easy to read if you come from that language heritage. However, it borrows from other languages wherever that supports the mission of making knowledge graphs easier to embed into the language.

Here's a simple class definition:

class Person {
    Name: string;
    Height: float;
    Age: float;
    Weight: float;
}

Functions are simple, and mostly follow the syntax of C#.

void main() {
    Person eric = new Person{
        Name = 'Eric Morecombe',
        Height = 1.93,
        Age = 65,
        Weight = 100
    };
    print(calculate_bmi(eric));
}

But they provide multi-clause function overloading, destructuring and pattern-matching/pre-conditions:

float calculate_bmi(Person p{
    Age = age | age > 60,
    Height = height,
    Weight = weight
    }) {
    return weight / (height * height) * 5;
}

float calculate_bmi(Person p) {
    return p.Weight / (p.Height * p.Height);
}