Web Development With HHVM and Hack 2: Expressions

Mike Abelar
3 min readFeb 20, 2020

--

This tutorial covers expressions in the Hack programming language. This is a continuation of the last tutorial.

Introduction

Before we develop web pages with Hack, we need to better understand the language itself. In this tutorial, we will begin exploring Hack by exploring expressions in Hack. An expression is any statement that will yield a value. We can begin experimenting by creating a new folder for this tutorial. Inside the folder, in your terminal, run:

touch .hhconfig

Which declares the folder to be a Hack project by creating a hhconfig file. Additionally, we will create a Hack file to experiment with:

touch expressions.hack

We will then put the following content in the file, similar to what we used in the last tutorial:

<<__EntryPoint>>
function main(): noreturn {
// code here
exit(0);
}

The code above will execute when we run the file, as defined in the main function. Note the line: // code here . This line is a comment, which means that the content on this line will not be executed. Therefore, we can write any content we want. In this case, we tell the programmer to put his/her code there.

Numerical Expressions

To start, Hack, like many other programming languages, allows us to manipulate numbers. Look at the following code below:

<<__EntryPoint>>
function main(): noreturn {
// code here
print(5+5);
exit(0);
}

We added the line print(5+5); . Note that all lines of code must end with semicolons. This line will print to the terminal the result of 5+5, which is 10. The expression needs to be inside the parentheses.

This time, we will not need to run a webserver to see the result of the code we write. Instead, we will do the following:

Run:

hh_client expressions.hack

in your terminal. This will run the type checker, which we will cover more extensively in future tutorials. The type checker checks your code and does its best to identify any errors with your code, so you can fix it before running.

Now, run:

hhvm expressions.hack

in your terminal. You should see a number 10 appear before your next terminal prompt. The hhvm command will actually run your code.

Now, we are ready to dive into more expressions:

Hack supports various mathematical operations:

print(5 — 5) Subtraction: prints 0

print(5 * 5) Multiplication: prints 25

print(5 / 5) Division: prints 1

prints(7 % 5) Modulo (remainder operation): prints 2

print(6 ** 3) Exponent operation: prints 216 (6³)

String Expressions

Another type of expression is a string. A string is a series of characters. Examples would be “abc” or “121414.” A string is enclosed by quotes (usually double quotes).

print("Hello World") will print “Hello World.”

Now, when running this code, you may have noticed that the print statement does not output the content on a new line. This can be annoying when trying to read the result. To do output to a new line, we need to use escape sequences. An escape sequence is a character that is proceeding by “\” which can generate expressions like a new line.

Try replacing the print expression with:

print("Hello World\n")

and run the code. You will see that Hello World still prints but is followed by a new line.

In the next tutorial, we will cover variables: https://medium.com/@mikeabelar/web-development-with-hhvm-and-hack-3-variables-eb07c33c7015

--

--