Hello World! Fantom heralds a new era of next generation programming.

An in-depth look at the Hello World program in the Fantom programming language.

The Hello World program is ubiquitous in the programming world, here we use it to take a tour of Fantom scripting:

Before we start, make sure you have Fantom installed on your computer.

The Main Method

A Fantom script is a text file with the extension .fan that contains (at least one) Fantom class definition and has a method called main(). Lets write a script called HelloWorld.fan:

class HelloWorld {
    static Void main() {
        echo("Hello World!")
    }
}

If we run the script with the fan command, Fantom will execute the main() method, which outputs those infamous words:

C:\>fan HelloWorld.fan
Hello World!

The main() method does not need to be static, for Fantom will instantiate a class for us!

class HelloWorld {
    Void main() {
        echo("Hello World!")
    }
}

The Main Method Arguments

We can pass in arguments from the command line:

class HelloWorld {
    Void main(Str[] args) {
        echo("Fantom says ${args[0]}!")
    }
}
C:\>fan HelloWorld.fan Wotever
Fantom says Wotever!

We can set a default list of arguments should none be specified:

class HelloWorld {
    Void main(Str[] args := ["Nothing"]) {
        echo("Fantom says ${args[0]}!")
    }
}
C:\>fan HelloWorld.fan
Fantom says Nothing!

Return Codes

If the main() method returns an Int then it is passed back to the command line as an error / return code:

class HelloWorld {
    Int main() {
        echo("Eeek! Error!")
        return 69
    }
}

The Main Class

If the script contains more than one class definition, then Fantom will run the first suitable Main() method it finds.

select all
class GoodBye {
    Void bye() {
        echo("Good Bye World!")
    }
}

class HelloWorld {
    Void main() {
        echo("Hello World!")
    }
}

If you run the above script, Fantom will ignore the Goodbye class because it does not have a main() method. Instead it will jump straight to HelloWorld class and print Hello World!.

See the official Fantom Hello World example and fan.exe documentation for more details.

Execute Scripts on Linux

To execute your fantom files as scripts on Linux, prefix your scripts with a Shebang:

#! /usr/bin/env fan

class HelloWorld {
    static Void main() {
        echo("Fantom says Hello!")
    }
}

Then give them executable rights:

$ chmod u+x Speak.fan

You can then run your script direct;

$ ./Speak.fan
Fantom says Hello!

But as a Linux user, you already knew that. Right!?

Execute Scripts on Windows

Windows users can also do the same by typing these lines into a command line:

assoc .fan=Fantom
ftype Fantom=%FAN_HOME%\bin\fan.exe "%1" %*
set pathext=%pathext%;.fan

Now you just need to type the name of the script to execute it:

C:\>HelloWorld
Fantom says Hello!

This comes in handy when building pods - for now you only need to type build!

Epilogue

Fantom scripts like Hello World are great when your entire program fits into a single file - but in the real world, that will rarely happen.

More usually, each individual .fan file contains a single class, and these classes are built into a project file called a pod.

To learn more, see How To Setup a Fantom Project.

Have fun!



Discuss