Hello World in Hack

Hello World in Hack Featured Image

With Facebook and Zuckerberg in the news latelyOpens in a new tab., I figured now would be a great time to look at one of their languages. In this tutorial, we’ll be learning how to implement Hello World in Hack.

Table of Contents

Hack Background

According to WikipediaOpens in a new tab., Hack is a dialect of PHP used by Facebook. Beyond that, Wikipedia was really only able to tell me that the open source language was released in 2014. So for once, I had to dig into the documentationOpens in a new tab..

Apparently, the most important features of Hack are:

  • Generics
  • Nullable Types
  • Type Annotations
  • Collections
  • Lambdas

Now, I think the most interesting feature has to be the type annotations. That’s because Hack is actually dynamically typed like Python.

However, if you’ve used Python, then you know that it has a relatively new type hinting feature. Typing hinting allows you to arbitrarily enforce static type checking in your code. I say arbitrarily because no one is forcing you to use type hints.

At any rate, Hack has essentially the same feature, but it’s restricted to parameters, class variables, and return values. For example:

function foo(int $x): int {
  return $x * 2;

Here we’ve defined some function foo which takes an integer parameter and returns an integer. Of course, nothing is stopping us from removing those annotations:

function bar($x) {
  return $x * 2;

Now, this function would run exactly the same except we wouldn’t discover any type issues until runtime.

So, what kind of typing system does Hack have? Is it static? How about dynamic? Well, there’s actually a new term for the kind of type system that languages like Hack and Python have. It’s called gradual typingOpens in a new tab., and it allows users to specify exactly when they want static or dynamic typing.

Once again, I think I’ve explored a topic a bit too deeply, so I’ll stop there.

Hello World in Hack

At long last, let’s take a stab at Hello World in Hack:

echo 'Hello, World!';

Right away, you should scream “that looks a lot like PHP!” After all, Hack is just a dialect of PHP.

That said, there is at least one obvious syntactic difference: the Hack tag. I can’t seem to figure out the formal name of the <?hh tag, so I’ve been calling it the Hack tag. At any rate, at least that’s different.

Beyond the tag, Hello World in Hack is exactly the same in Hello World in PHP. However, I will point out that you can’t mix HTML with Hack like you can with PHP, so that’s probably the biggest syntactic difference. Otherwise, both languages perform a similar function: backend web development.

How to Run the Solution

If we want to try this code, we can use an online Hack compilerOpens in a new tab.. We just have to drop the code into the editor and hit run:


Alternatively, we can download the Hack Virtual MachineOpens in a new tab. to run Hack code locally. From there, I recommend reading up on how to get started with HackOpens in a new tab.. Getting everything up and running is bit out of scope of this article.

Sample Programs in Every Language

And, that’s about it! Unfortunately, this implementation wasn’t that exciting. In the future, maybe we can try implementing some other functionalities.

Up next, I think I’ll be continuing this trend of newer programming languages. I’ve already set my eyes on languages like Julia and Elm, so we’ll see what happens with those.

As always, if you liked this article, be sure to share it. Also, if you have any recommendations for new languages to try, let me know in the comments. Thanks again for hanging out!

Sample Programs in Every Language (44 Articles)—Series Navigation

For 100 Days of Code, I’ve decided to implement a few sample programs in as many languages as possible. Each implementation details a brief history of the language and a description of the code.

The plan for the series is to explore the major general-purpose language like Java, Python, C, C++, and C#. From there, we’ll take a look at some sample programs in web development languages like Ruby, PHP, and JavaScript. As we continue, we’ll cover proprietary languages like Swift and Objective-C. Eventually, we’ll start to tackle less popular languages like Rust, x86, and Verilog. Finally, we’ll play around with some of the esoteric languages like Brainf*ck and LOLCODE.

Who knows? Maybe the Sample Programs in Every Language series will become so popular it’ll never end. To help this series grow, consider sharing it on social media with your friends. Or, if you have a language you want to see, drop your suggestion in the comments.

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Posts