Hello World in Hack

Hello World in Hack Featured Image

With Facebook and Zuckerberg in the news lately, 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 Wikipedia, 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 documentation.

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 typing, 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:

<?hh
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 compiler. We just have to drop the code into the editor and hit run:

Alternatively, we can download the Hack Virtual Machine to run Hack code locally. From there, I recommend reading up on how to get started with Hack. 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!

Series Navigation← Hello World in PascalHello World in Elm →
Advertisements

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.