Skip to main content
Version: Next

Your first program

In this chapter you write a small script, run it, turn it into R, and fix the first problems Vapour finds.

Check your installation​

Open a terminal and ask Vapour for its version.

vapour -version

If the command is not found, follow the install instructions first and make sure vapour is on your PATH.

Write a script​

Create a new directory, and in it a file called hello.vp. Vapour files always use the .vp extension.

hello.vp
func hello(who: char): char {
stopifnot(!missing(who))
return sprintf("Hello, %s!", who)
}

let message: char = hello("Vapour")
print(message)

Even this small script shows the main differences from R:

  • func declares a function. who: char says the argument must be a character vector, and : char after the parentheses says the function returns one too.
  • return is a keyword, not a function, and it is required.
  • let message: char declares a variable of type char.
  • stopifnot(!missing(who)) checks that the argument was passed. Without it, Vapour warns you, see functions.

Run it​

Run the script with the -run-only flag. Vapour checks the code, transpiles it in memory, and runs it in R without writing any file.

vapour -infile=hello.vp -run-only
✓ files successfully transpiled!
[1] "Hello, Vapour!"

Transpile it to R​

When you are happy with the code, drop -run-only to write the R code to a file. Use -outfile to name it, otherwise it is called vapour.R.

vapour -infile=hello.vp -outfile=hello.R

Open hello.R: it is ordinary R code that you can run with Rscript, source, or put in a package.

func hello(who: char): char {
stopifnot(!missing(who))
return sprintf("Hello, %s!", who)
}

let message: char = hello("Vapour")
print(message)

The types are gone: they only exist in Vapour, where they are checked.

Make a mistake​

Now break the script on purpose: give message the wrong type.

func hello(who: char): char {
stopifnot(!missing(who))
return sprintf("Hello, %s!", who)
}

let message: int = hello("Vapour")
print(message)

Run it again.

vapour -infile=hello.vp -run-only
[ERROR]	hello.vp:6:1 `message` expects `int`, got `char`
x failed to transpile files!

Each problem is reported on one line:

[SEVERITY]  file:line:column  message

This one is an ERROR: the code is wrong, so Vapour stops and does not run or write anything. Change int back to char and it runs again.

Warnings don't stop you​

Remove the stopifnot line and run the script again.

func hello(who: char): char {
return sprintf("Hello, %s!", who)
}
[WARN]	hello.vp:2:32 `who` might be missing
✓ files successfully transpiled!
[1] "Hello, Vapour!"

This time the script still runs. A WARN points at code that will probably fail: here, calling hello() without an argument would be an error in R. Vapour reports four severities, ERROR, WARN, INFO, and HINT, and only errors stop transpilation. The diagnostics chapter lists all of them.

Only check​

To check your code without running or writing anything, use -check-only.

vapour -infile=hello.vp -check-only

This is handy in CI, or when you just want to know if the code is valid.

Many files​

Real projects have more than one file. Point Vapour at a directory with -indir and every .vp file in it is read, recursively and in alphabetical order, as one program.

vapour -indir=src -outdir=R

Because files are read in alphabetical order, something must be declared in a file that comes before the files that use it.

Recap​

  • Vapour code lives in .vp files.
  • -run-only checks and runs, -check-only only checks, and with neither flag Vapour writes the R code.
  • Errors stop transpilation, warnings, info, and hints do not.
  • See the CLI page for every flag, and the REPL to try code interactively.

Next, let's look at how variables are declared.