current: 1.0.2

Open Source Approved

SourceForge Logo



Arrow Via Dots From Logo

Examples

This page should hopefully provide you with a set of examples that show off the various features of ferite and allow you to get a feel for what the language looks like before you delve into the programming language manual.

We start of with simple expressions with variables, functions, objects, classes, closures, and namespaces. Having done a little on structure, we look at various tricks you can exploit in ferite.

We hope that you enjoy the ride and end up with a better feel for ferite!

Hello World

We start out with the famous Hello World program. The program prints out the words Hello World to standard out (where most text ends up).
uses "console";
Console.println( "Hello World" );
Output:
Hello World

Numbers, Strings, Arrays

The code:
number a;
string b;
array c;

a = 2;
b = "2";
c = [ a, b ];

Console.println( a );
Console.println( a + 2 );
Console.println( a * 4 );
Console.println( b );
Console.println( c );
Output:
2
4
8
2
[ 2, '2' ]
We can even place values in strings:
number x = 10;
string y = "The value of x=$x";
Taking it a little further, we can even place expressions in there!
number x = 10, y = 20;
string z = "The value of x plus y = ${x+y}";

Functions

The Code:
function f( number x ) {
    if( x > 1 ) {
        return f( x - 1 ) * 2;
    }
    return 1;
}
Console.println( f(10) );
Output:
512

Namespaces

What does a namespace look like ?
namespace A {
    function f() {
        return "A.f";
    }
}

namespace modifies A {
    function g() {
        return "A.g";
    }
} 

Classes

What does a class look like ?
class A {
    string value;
    
    function constructor() {
        .value = "Class.A";
    }
    
    function f() {
        return .value;
    }
} 

Protocols

A protocol is a contract, a bit of code that forces parts of a class to take on a certain structure.
protocol C {
    function getX();
    function getY();
}

class A implementes C {
    function getX() { return "X"; }
    function getY() { return "Y"; }
}

Objects

Object are just instances of a class. They look like this:
object o = new A();

Closures

Can you do the lambda ? ferites closures are the same as lambda terms, they do capturing!
number x = 10, y = 15, z = 30;
object o = closure {
    return x + y + z;
};
Or they can do iteration!
Array.each( [ 1, 2, 3 ] ) using ( value ) {
    Console.println( value );
};

Variable Argument Lists

Variable argument lists ? Easy!
function test( string fmt, ... ) {
    array args = arguments();
    number i = 0;

    Console.println( "test() called with ${Array.size(fncArgs)} args" );
    Console.println( "format: $fmt" );

    for( i = 0; i < Array.size(fncArgs); i++ )
    {
        Console.println( "Arg[$i]: ${fncArgs[i]}" );
    }
}

Exception Handling

Sometimes problems can occur, and sometimes you want to catch the issues!
iferr {
    /* some code that might go wronf */
} fix {
    /* Tidy up! */
} else {
    /* If we have no error, lets do some different cleaning up */
}

Loops

And to finish this whistle-stop tour of ferite, here are the standard issue loops:
for( number i = 0; i < 10; i++ ) {
    Console.println( "Hi for the $i" );
}

i = 0;
while( i < 10 ) {
    Console.println(i);
    i++;
}

i = 0;
do {
    Console.println(i);
} while( i++ < 10 );
ferite et al © 2000-2005, Chris Ross