Hack (Lenguaje de Programacion)

Post on 14-Jul-2015

101 views 14 download

Transcript of Hack (Lenguaje de Programacion)

Hack

@Quidi90@davidmogar @elwinlhq

HACK 101De PHP a Hack en 20 minutos

2014

+ PHP + Java + C#

+ PHP

<?hh echo 'Lets\'s Hack';

<?php echo 'Yup, still valid';

La mayoría delos archivos PHP

son válidos en Hack

TODO PUEDE FALLAR

Slide by Aaron Weyenberg

1

Estático y dinámico

<?php

function sumar_uno($n) {return $n + 1;

}

function test() {$mi_cadena = 'hola';

sumar_uno($mi_cadena);}

<?hh

function sumar_uno(int $n): int {return $n + 1;

}

function test(): void {$mi_cadena = 'hola';

sumar_uno($mi_cadena);}

2

Modos

<?hh // strict <?hh // partial <?hh // decl

<?hh

function unsafe_foo(int $x, int $y): int {if ($x > $y) {

// UNSAFEreturn "Nadie va a comprobar mi tipo";

}

return 34; // NO está cubierto por UNSAFE}

3

Constructores

<?hh

class Punto {

public function __construct(private float $x,private float $y

) { }

}

<?php

class Punto {

private $x;private $y;

function __construct($x, $y) {$this->x = $x;$this->y = $y;

}

}

4

Colecciones

<?hh

function test(): int {$vector = Vector {1, 2, 3 };

$sum = 0;foreach ($vector as $val) {

$sum += $val;}

return $sum;}

<?hh

function test() {$mapa = Map{"A" => 1, "B" => 2, "C" => 3 };

$mapa["D"] = 4;$mapa["E"] = 5;

$map->remove("B");foreach ($mapa as $clave => $valor) {

echo $clave . ": " . $valor;}

}

SetPair

ImmVectorImmMapImmSet

5

Expresiones Lambda

<?hh

$imprimir_mapa = ($nombre, $mapa) ==> {echo "El mapa $nombre tiene:\n";foreach ($mapa as $clave => $valor) {

echo " $clave => $valor\n";}

};

$imprimir_mapa("Mi Mapa",Map {'a' => 'b', 'c' => 'd'},

);

6

Nullable

<?hh

function consultar(): ?DBData {$consulta = mysql_query('SELECT …');if ($consulta === false || mysql_num_rows($consulta) == 0) {

return null;}

return new DBData(mysql_fetch_array($consulta));}

function test() {$datos = consultar();

}

7

Generics

<?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;

}

8

Enumeraciones

<?hh

enum DiaDeLaSemana: int {Lunes = 0;Martes = 1;Miercoles= 2;Jueves = 3;Viernes = 4;Sabado = 5;Domingo = 6;

}

function foo(): DiaDeLaSemana {return DiaDeLaSemana::Martes;

}

9

Shapes

<?hh

type Punto = shape('x' => int, 'y' => int);

function productoEscalar(Punto $inicio, Punto $fin): int {var_dump($inicio);var_dump($fin);return $inicio['x'] * $fin['x'] + $inicio['y'] * $fin['y'];

}

function test(): void {echo productoEscalar(shape('x' => 3, 'y' => 3), shape('x' => 4, 'y' => 4));

}

10

Tuplas

function foo(): (string, int) {$tupla = tuple("Hola", 3);$tupla[1] = 4;return $tupla;

}

11

Continuaciones

<?hh

function producirInfinitosEnteros(): Continuation<int> {$i = 0;while (true) {

yield $i++;}

}

$generador = producirInfinitosEnteros();

foreach ($generador as $valor) {echo "$valor\n";

}

12

Traits

<?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

}

13

Async

<?hh

class Foo {}class Bar {

public function getFoo(): Foo { return new Foo(); }}

async function generar_foo(int $valor): Awaitable<?Foo> {if ($valor === 0) { return null; }

$bar = await gen_bar($valor);if ($bar !== null) {

return $bar->getFoo();}

return null;}

async function generar_bar(int $valor): Awaitable<?Bar> {if ($valor === 0) { return null; }

return new Bar();}

generar_foo(4);

14

Características--

● 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

¿Y ahora?

Hack

@Quidi90@davidmogar @elwinlhq