Este documento presenta una introducción de 20 minutos a Hack, un lenguaje de programación similar a PHP pero con tipos estáticos. Explica las principales características de Hack como su sistema de tipos, colecciones, expresiones lambda, tipos opcionales, generificación, enumeraciones, shapes y continuaciones. El objetivo es mostrar cómo Hack mantiene la compatibilidad con PHP pero agrega características para mejorar la seguridad y legibilidad del código.
16. <?hh
function unsafe_foo(int $x, int $y): int {
if ($x > $y) {
// UNSAFE
return "Nadie va a comprobar mi tipo";
}
return 34; // NO está cubierto por UNSAFE
}
28. <?hh
class Almacen<T> {
public function __construct(
private T $dato
) {}
public function get(): T {
return $this->dato;
}
public function set(T $nuevo_dato): void {
$this->dato = $nuevo_dato;
}
}
function test(): Almacen<int> {
$dato = 'Hola mundo!';
$almacen = new Almacen($dato);
return $almacen;
}
38. <?hh
interface Bar {
public function primero(Vector<int> $vector): int;
public function get(): int;
}
trait Foo implements Bar {
private int $valor = 5;
private function getValor(): int {
return $this->valor;
}
public function get(): int {
return $this->getValor();
}
public function primero(Vector<int> $vector): int {
if (count($vector) > 0) {
return $vector[0];
}
return -1;
}
}
class FooBar implements Bar {
use Foo;
private Vector<int> $vector;
public function __construct() {
$this->vector = Vector {};
$this->vector[] = $this->get();
}
public function segundo(): int {
return $this->primero($this->v);
}
}
function test() {
$foobar = new FooBar();
var_dump($foobar->segundo()); // 5
}
42. ● Construcciones poco frecuentes (goto, if:...endif)
● AND, OR, XOR
● Referencias
function foo(&$x)
● Uso del simbolo @ para silenciar errores
● break N y continue N
● Globales (global $x;)
● Variables compuestas por variables
$a = "hola"; $$a = "mundo"; echo "$a + $hola";
● Mezcla de etiquetas HTML y de código -> XHP