HHVM and Hack

Might These Be The Droids We Are Looking For?

Nikola Plejić

BYOPL (Build Your Own Programming Language)

BYOPL (Build Your Own Programming Language)

static typing*

or at least optional static typing

why?

are our current tools so inadequate?

solution?

rm -rf project/ && git init project/

(not really.)

what's up with PHP?

some very cool stuff

what's up with PHP?

some not-so-cool stuff

Facebook to the rescue?

HHVM?

Hack?

HHVM

what's the fuss, anyway?

multiple implementations are the norm

different implementations — different focus

competition, diversity

History

(disclaimer: not how it actually happened.)

2010: HipHop for PHP

2010: HipHop for PHP

2011 / 2012: HHVM

late 2012: HHVM performance beats hphpc

Y U NO JIT?

HHVM Exec Model

HHVM Exec Model

HHVM Exec Model

HHVM vs. Zend

<?hh

"The goal of Hack to offer developers a way to write cleaner, safer and refactorable code while trying to maintain a level of compatibility with current PHP codebases. The primary way that this goal is achieved is to provide developers a way to annotate PHP functions and classes with type information, providing a type checking tool to validate those annotations."

Key Features

Type Checking

Type Checking


<?hh

function x(int $a) {
  return $a * 2;
}

print(x(2));

// 4

Type Checking


<?hh

function x(int $a) {
  return $a * 2;
}

print(x(null));

// Fatal error: Argument 1 passed
// to x() must be an instance of int,
// null given in /mnt/hhvm/index.hh
// on line 5

Hack Modes

Cup<T>

Cup<T>

<?hh

// deceptively useless example
class Box<T> {
  public T $value;
  public function __construct(T $v) {
    $this->value = $v;
  }
}

// there are no typed references
// in Hack. yet, objects are
// passed by reference.

Nullable

MOAR NULLZ!!!111

Nullable

<?hh

function setAge(int? $age, Person $p) {
  if (is_null($age)) {
    $p->setAge(Age::UNKNOWN);
  } else {
    $p->setAge($age);
  }

  return $p;
}

Type Aliases

<?hh

type Age = int;

function setAge(Age? $age, Person $p) {
  $p->setAge($age);
  return $p;
}

// we can pass a regular int here
setAge(21, $person);

"Opaque" Type Aliases

<?hh

// one.php
newtype myInt = int;
function dbl(myInt $i) { return $i * 2; }

// two.php - type checker balks
function triple(myInt $i) { return $i * 3; }

actual usage for Type Aliases

<?hh

type Point2D = (int, int);

function quux(Point2D $p) { return $p; }
// instead of...
function quux((int, int) $p) { return $p; }

Proper Lambdas

<?php
$param = true;

// "use": explicit closure wannabes

$x = array_map(function($a) use /* WAT */ ($param) {
  return ($param === true) ? ($a * 2)
                           : ($a * 3);
}, $my_important_array);

Proper Lambdas

<?hh

$param = true;

$x = array_map($a ==> {
  return ($param === true) ? ($a * 2)
                           : ($a * 3)
}, $my_important_array);

Some Other Features

docs, community, tools

running HHVM & Hack

Final Notes

Thank you!

nikola.plejic.com/talks/hhvm

nikola.plejic.com / nikola@plejic.com