polar

Hello World

The smallest Polar program looks like this:

module Main

functions
  main() {
    Log.info("Hello from Polar")
  }

exports
  main

Save it as hello.px and run it:

$ polar run hello.px
Hello from Polar

It prints a line to the console. Let's go through it piece by piece.

The module line

Every Polar file starts by naming its module. Module names start with a capital letter. A file run on its own is usually called Main, but any name works.

Zones

After the module line, a file is split into zones. A zone is a heading like functions or exports, followed by indented declarations. This file has two:

  • functions holds the function main.
  • exports lists what the module makes public. polar run looks for an exported main and calls it.

Zones replace keywords that other languages put in front of every declaration, like function or export. We'll meet all of them in Files and Zones.

Printing

Log.info prints a string, and Log.error prints to the error output. Both are built in, so you don't need to import them.

Note: Indentation matters. Declarations inside a zone are indented by two spaces, and the body of a function goes in braces. polar fmt fixes the layout for you.

What it compiles to

Polar writes plain JavaScript. To see it, build the file:

$ polar build hello.px --out dist
built 1 file in 5ms
import * as $rt from "./_polar/runtime.js";

export function main() {
  return $rt.Log.info("Hello from Polar");
}
//# sourceMappingURL=hello.js.map

The exported function is a real ES module export. $rt is the small runtime Polar copies next to your code.

Make a change

Try changing the message, or calling Log.info a second time. Then try passing it a number, like Log.info(42). Polar refuses to compile that, because Log.info expects a String. Catching mistakes like this before anything runs is the main thing Polar does.

Next, let's look at files and zones in more detail.