Hello World: How It Works

Let's look at our helloworld code again:

actor Main
  new create(env: Env) =>
    env.out.print("Hello, world!")

Let's go through that line by line.

Line 1

actor Main

This is a type declaration. The keyword actor means we are going to define an actor, which is a bit like a class in Python, Java, C#, C++, etc. Pony has classes too, which we'll see later.

The difference between an actor and a class is that an actor can have asynchronous methods, called behaviours. We'll talk more about that later.

A Pony program has to have a Main actor. It's kind of like the main function in C or C++, or the main method in Java, or the Main method in C#. It's where the action starts.

Line 2

  new create(env: Env) =>

This is a constructor. The keyword new means it's a function that creates a new instance of the type. In this case, it creates a new Main.

Unlike other languages, constructors in Pony have names. That means there can be more than one way to construct an instance of a type. In this case, the name of the constructor is create.

The parameters of a function come next. In this case, our constructor has a single parameter called env that is of the type Env.

In Pony, the type of something always comes after its name, and is separated by a colon. In C, C++, Java or C#, you might say Env env, but we do it the other way around (like Go, Pascal, and a bunch of other languages).

It turns out, our Main actor has to have a constructor called create that takes a single parameter of type Env. That's how all programs start! So the beginning of your program is essentially the body of that constructor.

Wait, what's the body? It's the code that comes after the =>.

Line 3

    env.out.print("Hello, world!")

This is your program! What the heck is it doing?

In Pony a dot is either a field access or a method call, much like other languages. If the name after the dot has parentheses after it, it's a method call. Otherwise, it's a field access.

So here, we start with a reference to env. We then lookup the field out on our object env. As it happens, that field represents stdout, i.e. usually it means printing to your console. Then, we call the print method on env.out. The stuff inside the parentheses are the arguments to the function. In this case, we are passing a string literal, i.e. the stuff in double quotes.

In Pony, string literals can be in double quotes, in which case they follow C/C++ style escaping (using stuff like \n), or they can be in "triple double" quotes, like a Python triple quoted string, in which case they are considered raw data.

What's an Env, anyway? It's the "environment" your program was invoked with. That means it has command line arguments, environment variables, stdin, stdout, and stderr. Pony has no global variables, so these things are explicitly passed to your program.

That's it!

Really, that's it. The program begins by creating a Main actor, and in the constructor, we print "Hello, world!" to stdout. Next, we'll start diving into the Pony type system.

results matching ""

    No results matching ""