Tutorial: Hello World - Manhunter07/MFL GitHub Wiki

In this tutorial you will learn how to use MFL to create the most simple, but perhaps most iconic kind of program: A so-called Hello World application. Its purpose is nothing more but to return (and preferrably also, print) the text Hello World as output. In order to make it a bit more complicated, we will however also introduce a few other nice things we can do with out Hello World text.

Chosing the most senseful data type

In order to print out written text, we should use the string data type. We can insert a direct string value into an expression by using the string notation (double quotes). The string representation of Hello World would therefore be "Hello World", that creates an anonymous string constant of the length 11.

Returning a value

Since our program should return a value, we should enter a the string as a simple expression for the compiler. In that case we would not use any keyword in the beginning, but only the string notation. Doing that returns the string value "Hello World" for the application.

Using a named constant

See also: Constants

Instead of using an unnamed constant (direct value) of a string, we can also declare a (named) string constant and return it later. We could then use the same value twice or more without having to type it again each time. Declaring a constant with the name HelloWorld is done with an additional command before the final output that starts with the const keyword, followed by an equal sign and the value (in this case, "Hello World"):

const HelloWorld = "Hello World"

We then need to use that constant instead of our direct value to be returned:

HelloWorld

Using concatenation

See also: Operators#Strings

Strings can be concatenated by using the addition operator (plus, +). Subtraction and other common operations not possible for strings. If we now declare two different constants for the words Hello and World, we can build a new, combined string out of them:

const Hello = "Hello"
const World = "World"

We can now simply build our own Hello World using these named constants and a space in between:

Hello + " " + World

This will return "Hello World" as well.

Using the built-in function Join

Instead of concatenating three strings together, we may also just join them using the (built-in) Join function from the System package. Since it is in the System package and we have no own identifier declared with this name, we do not need to use its qualified name System.Join, but simply its (unqualified) identifier Join. As arguments, this function takes an array of strings plus a single string which is stuffed between the elements of the first argument. If the second argument is omitted, the elements are concatenated without anything in between. In this case, we need an array with Hello and World as elements for the first argument and a string containing a space for the second argument. Just like a string, an array can also be created in-line using the array notation (surrounded by brackets, separated by comma): [Hello, World]. For the second argument, we use a string with a space as its only character: " ".

Combining these things with the Join function will give us this expression, that will, just like the previous ones, also return us Hello World:

Join([Hello, World], " ")