44
Hack @Quidi90 @davidmogar @elwinlhq

Hack (Lenguaje de Programacion)

Embed Size (px)

Citation preview

Page 1: Hack (Lenguaje de Programacion)

Hack

@Quidi90@davidmogar @elwinlhq

Page 2: Hack (Lenguaje de Programacion)

HACK 101De PHP a Hack en 20 minutos

Page 3: Hack (Lenguaje de Programacion)

2014

Page 4: Hack (Lenguaje de Programacion)

+ PHP + Java + C#

Page 5: Hack (Lenguaje de Programacion)

+ PHP

Page 6: Hack (Lenguaje de Programacion)
Page 7: Hack (Lenguaje de Programacion)

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

Page 8: Hack (Lenguaje de Programacion)

<?php echo 'Yup, still valid';

Page 9: Hack (Lenguaje de Programacion)

La mayoría delos archivos PHP

son válidos en Hack

Page 10: Hack (Lenguaje de Programacion)

TODO PUEDE FALLAR

Slide by Aaron Weyenberg

Page 11: Hack (Lenguaje de Programacion)

1

Estático y dinámico

Page 12: Hack (Lenguaje de Programacion)

<?php

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

}

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

sumar_uno($mi_cadena);}

Page 13: Hack (Lenguaje de Programacion)

<?hh

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

}

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

sumar_uno($mi_cadena);}

Page 14: Hack (Lenguaje de Programacion)

2

Modos

Page 15: Hack (Lenguaje de Programacion)

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

Page 16: Hack (Lenguaje de Programacion)

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

Page 17: Hack (Lenguaje de Programacion)

3

Constructores

Page 18: Hack (Lenguaje de Programacion)

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

}

}

Page 19: Hack (Lenguaje de Programacion)

4

Colecciones

Page 20: Hack (Lenguaje de Programacion)

<?hh

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

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

$sum += $val;}

return $sum;}

Page 21: Hack (Lenguaje de Programacion)

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

}

Page 22: Hack (Lenguaje de Programacion)

SetPair

ImmVectorImmMapImmSet

Page 23: Hack (Lenguaje de Programacion)

5

Expresiones Lambda

Page 24: Hack (Lenguaje de Programacion)

<?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'},

);

Page 25: Hack (Lenguaje de Programacion)

6

Nullable

Page 26: Hack (Lenguaje de Programacion)

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

}

Page 27: Hack (Lenguaje de Programacion)

7

Generics

Page 28: Hack (Lenguaje de Programacion)

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

}

Page 29: Hack (Lenguaje de Programacion)

8

Enumeraciones

Page 30: Hack (Lenguaje de Programacion)

<?hh

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

}

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

}

Page 31: Hack (Lenguaje de Programacion)

9

Shapes

Page 32: Hack (Lenguaje de Programacion)

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

}

Page 33: Hack (Lenguaje de Programacion)

10

Tuplas

Page 34: Hack (Lenguaje de Programacion)

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

}

Page 35: Hack (Lenguaje de Programacion)

11

Continuaciones

Page 36: Hack (Lenguaje de Programacion)

<?hh

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

yield $i++;}

}

$generador = producirInfinitosEnteros();

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

}

Page 37: Hack (Lenguaje de Programacion)

12

Traits

Page 38: Hack (Lenguaje de Programacion)

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

}

Page 39: Hack (Lenguaje de Programacion)

13

Async

Page 40: Hack (Lenguaje de Programacion)

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

Page 41: Hack (Lenguaje de Programacion)

14

Características--

Page 42: Hack (Lenguaje de Programacion)

● 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

Page 43: Hack (Lenguaje de Programacion)

¿Y ahora?

Page 44: Hack (Lenguaje de Programacion)

Hack

@Quidi90@davidmogar @elwinlhq