Log in - Edit - History

Plof Variables and Functions

Variables

In Plof, variables can be declared with the "var" keyword. Try running this simple example:

var helloWorldString = "Hello, world!\n"
Stdout.write helloWorldString

Note that a semicolon is not needed to separate Plof statements -- only newlines. We can also operate on variables with methods depending on their type; in this case, we are working with strings. So this program will also output "Hello, world!":

var helloWorldString = "Hello, world!"
var newline = "\n"
Stdout.write (helloWorldString ~ newline)

Different methods and operators exist for different object prototypes, which will be explained in a later tutorial.

Functions

Declaring a named Plof function looks fairly similar to declaring a variable. The function itself is surrounded by curly braces:

var helloWorld = {
    Stdout.write "Hello, world!\n"
}

helloWorld()

Functions can also take arguments. For this to happen, parenthesis must be used to describe the arguments before the function is defined. Look at this example:

var hello = (thing)  {
    Stdout.write ("Hello, " ~ thing ~ "!\n")
}

hello "world"

Note that parenthesis are not needed to call a function, but can be used. Thus, hello "world" is the same as hello("world"), and for multiple arguments spaces can be used, so func arg1 arg2 is the same as func(arg1, arg1). To create a function with multiple arguments, use commas. For example:

var greeting = (greeting, thing)  {
    Stdout.write (greeting ~ ", " ~ thing ~ "!\n")
}

greeting "Hello" "world"

Now for a possible problem -- try greeting 1 2. It will say "Variable opConcat undefined" because the concatenation operator (~) is meant for strings and not for integers. To ensure that the function is used correctly, we can write some smarter code:

var greeting = (greeting as String, thing as String)  {
    Stdout.write (greeting ~ ", " ~ thing ~ "!\n")
}

greeting "Hello" "world"

This ensures type safety (at runtime) by using the keyword "as" which makes sure that an object is of a certain type; in this case, String. If you run greeting 1 2, you will get something like "Cast failed", because right now there isn't a NativeInteger to String cast operator (NativeInteger is what all integer variables are) -- if this is ever implemented, you should see "1, 2!" as output. This is Plof making sure that arguments are of the correct type (by use of casting).

Plof allows the "return" statement in a function. So this code will work as expected:

var greetingify = (greeting as String, thing as String)  {
    return (greeting ~ ", " ~ thing ~ "!\n")
}

Stdout.write (greetingify "Hello" "world")

However, the return statement is not necessary at the end of a function -- if no return statement is encountered, the value of the last statement is returned. So the previous code is equivalent to this:

var greetingify = (greeting as String, thing as String)  {
    (greeting ~ ", " ~ thing ~ "!\n")
}

Stdout.write (greetingify "Hello" "world")

In the next tutorial we'll take a look at some of the Plof language constructs and other random stuff.

Next tutorial