Hack: Asynchronous programming

Hack has introduced asynchronous programming. This is really great but it is not really documented at the moment. Not at all actually… I’ve done some experimenting and I will try to introduce you to the new concept. There is two new keywords async and await....

Tuple

A tuple is very similar to a PHP array and a shape. The key feature of tuples is that they are immutable. You may not change the length or the types of the elements in a tuple. The values within the tuple are mutable. public function test(): void { $tup = tuple('3',...

Shape

Hack introduced a concept called Shapes. It is an object like the associative PHP array but with predefined array keys. If you are familiar with C you might think of a shape like a struct. So what is the use case for this? Why should we complicate things with Shapes...

Why collections?

Hack has introduced some new object to replace the PHP array. These collections are Vector, Set, Map, Pair and some more. If you are familiar with Java or C# you probably know the benefits of each of these collections already. You might be thinking: Why would you want...

Hack Lambda expressions

Hack is all about making it easier for the developer to write good code. There has been some improvement of the PHP anonymous function or closer as they also are called. An anonymous function is a function without a name (dah!). You may use them as a parameter to some...

Hack constructors

Hack got a lot of features and improvements of different kinds. It comes with some syntactic sugar. The constructor is a subject for a syntactic sugar improvement. The PHP-way of writing a class might look like this: <?php class Foobar { protected string $name;...

Hack modes

When you are using Hack you are running in one of 3 different modes. The modes are telling the type checker how strict (or forgiving) it should be when checking the code. The modes are strict, partial and decl. In addition to this there modes, there is a declaration...

Using generics in Hack

Hack has introduced some new collection objects that will be a replacement for the array(). But what if you feel that something is missing? What if you have a problem that is easiest solved with another type of object? This is where we use generics. Say that you want...

Method declarations in Hack

This post will go more in details of my last post about types. After reading that last post you know that a method declaration in Hack will look like this: //hack class Foo { public function bar (int $var): string { return "Number: ".$var; } } I will dig in...

Hack types

The biggest difference between Hack and PHP is that PHP is using dynamic typing and Hack is statically typed. Java and C# is other languages that is statically typed. This means that the type on variables must be defined before runtime. Example of types are string,...