hack in 60 seconds - nyholm.tech · agenda • background • my 60 seconds • features •...

49
Hack in 60 seconds Tobias Nyholm

Upload: others

Post on 24-Jul-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

Hack in 60 secondsTobias Nyholm

Page 2: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

Agenda

• Background

• My 60 seconds

• Features

• Questions

• How could you get started?

Page 3: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

About me

• Tobias Nyholm, @tobiasnyholm

• Happyr

• Certified Symfony developer

• Co-host of Sound of Symfony podcast

Page 4: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

Facebook had a problem

Page 5: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

HipHop

• HPHPc

• HPHPi & HPHPd

• JIT

• HHVM

Page 6: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

FastCGI mode

• Use as PHP-FPM

• Linux packages

• http://hhvm.com/blog/1817/fastercgi-with-hhvm

Page 7: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

Wordpress startpage

PHP 5.5 HHVM

Request per second 23 184

Time per request 43 ms 5 ms

Page 8: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

Fibonacci

PHP 5.5 HHVM

FIB(5) 13 789 8 842

FIB(15) 3 202 8 892

FIB(25) 119 5 581

FIB(30) 8 737

(Request per second)

Page 9: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

Your application will run about 5 times faster

Page 10: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

Wikipedia

http://hhvm.com/blog/7205/wikipedia-on-hhvm

Page 11: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

Wikipedia

http://hhvm.com/blog/7205/wikipedia-on-hhvm

Page 12: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

What about PHP7?

Page 13: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?
Page 14: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

Why is HHVM good for PHP?

Page 15: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

History of PHP• PHP 3 - 1998

• PHP 4 - late 1998

• PHP 5.0 - 2004

• PHP 7 - late 2015

• PHP 5.1 - 2005

• PHP 5.2 - 2006

• PHP 5.3 - 2009

• PHP 5.4 - 2012

• PHP 5.5 - 2013

• PHP 5.6 - 2014

Page 16: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

What about Hack?

Page 17: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

123 == “123foo” "6" == " 6"

"133" == "0133"

133 ! = 0133

“025” == 031NULL < -1NULL == 0

“foo” == TRUE“foo” == 0TRUE != 0

“123” != “123foo”

Page 18: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

Hack

• Backwards compatible with PHP

• Slightly faster on HHVM

• Syntactic sugar

• Gradually typed

Page 19: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

Static typed languages• C, Go, Java, Haskell

public class Foobar{ String foo; int bar;

String baz(int i){ return “Biz”; } }

func main() { var i, j int = 1, 2 k := 3 c, python, java := true, false, "no!"

fmt.Println(i, j, k, c, python, java) //output: 1 2 3 true false no! }

Page 20: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

Dynamic typed language• PHP, Ruby, Python, Javascript

class Storage { private $foo;

String set($i) { $this->foo=$i } }

$storage = new Storage(); $storage->set(4711); $storage->set(‘Foobar’); $storage->set(array(‘foo’)); $storage->set(new User()); $storage->set(null);

Page 21: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

But what if…$storage = new Storage(); $storage->set(4711);

// … $array = $storage->get(); echo $array[0];

// … $user = $storage->get(); echo $user->getUsername();

Page 22: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

Hack modes

• Strict - Type check everything

• Partial - Check whatever is annotated

• Decl - Check nothing

Page 23: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

Hack modes<?hh //strict

Page 24: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

–The audience

“Show some code”

Page 25: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

<?php

class MessageProvider { private $counter=0; private $message=“”;

public function set($text) { $this->message = $text; $this->counter++; return $this->counter; } }

Page 26: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

<?hh

class MessageProvider { private $counter=0; private $message=“”;

public function set($text) { $this->message = $text; $this->counter++; return $this->counter; } }

Page 27: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

<?hh

class MessageProvider { private int $counter=0; private string $message=“”;

public function set($text) { $this->message = $text; $this->counter++; return $this->counter; } }

Page 28: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

<?hh

class MessageProvider { private int $counter=0; private string $message=“”;

public function set(string $text) { $this->message = $text; $this->counter++; return $this->counter; } }

Page 29: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

<?hh

class MessageProvider { private int $counter=0; private string $message=“”;

public function set(string $text): int { $this->message = $text; $this->counter++; return $this->counter; } }

Page 30: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

<?hh

function main() { $mp= new MessageProvider(); $var = $mp->set(“hello”); $mp->set($var); }

main();

/index.php:6:14,17: Invalid argument /Mp.php:6:25,30: This is a string /Mp.php:6:40,42: It is incompatible with an int

Page 31: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

Syntactic sugar<?hh

class MessageProvider { private int $counter=0; private string $message=“”;

public function __construct( int $cnt, string $msg ) { $this->counter = $cnt; $this->message = $msg; } }

Page 32: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

Syntactic sugar

<?hh

class MessageProvider {

public function __construct( private int $counter, private string $message ) { } }

Page 33: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

Syntactic sugar

Page 34: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

//$arr = array('foo', 'hello', 'bazbar');

Syntactic sugar<?php

$arr = array('hello', 'bazbar', 'foo'); usort($arr, function($a, $b) { return strlen($a)-strlen($b); });

<?hh

$arr = array('hello', 'bazbar', 'foo'); usort($arr, ($a, $b) ==> strlen($a)-strlen($b));

Page 35: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

<?php

$input = array(2, 3, 5, 6, 7); $factor = 3;

$result = array_filter( array_map( function($a) use ($factor) { return $a * $factor; }, $input ), function($a) { return $a % 2 == 1; } ); //$result = array(9, 15, 21);

Page 36: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

<?hh

$input = array(2, 3, 5, 6, 7); $factor = 3;

$result = array_filter( array_map($a ==> $a * $factor, $input), $a ==> $a % 2 == 1 );

//$result = array(9, 15, 21);

Page 37: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?
Page 38: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

Traitstrait Foo { public function bar() { return $this->biz(); } }

class Baz { use Foo; public function biz() {} }

Page 39: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

Traits with requirementstrait Foo { require extends A; require implements B; public function bar() { return $this->biz(); } }

Page 40: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

Nullable<?hh

function foo(int $x): int { return $x + 4711; }

function main() { $y = foo(null); }

main();

Page 41: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

Nullable<?hh

function foo(int $x): int { return $x + 4711; }

function main() { $y = foo(null); }

main();

Page 42: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

Arrays are great!$vector = [4,7,1,1]; $map = array(‘foo’=>47, ‘bar’=>11); $set = array_unique(array(4,7,1,1)); $object = array(

‘name’=>’Tobias’, ‘age’=>25, ‘twitter’=>’@tobiasnyholm’,

);

$gender = $object[‘gender’];

Page 43: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

Shape

type Heading = shape( 'nr' => int, 'full' => string, 'content' => string, 'attributes' => string, );

Page 44: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?
Page 45: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?
Page 46: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

Genericsclass Storage<T> { protected T $var;

public function __construct(T $var) { $this->var = $var; }

public function get(): T { return $this->var; }

public function set(T $var) { $this->var = $var; } }

function getIntStorage(): Storage<int> { return new Storage(4711); } function getStrStorage(): Storage<string> { return new Storage("Hello"); }

function main() { $int = getIntStorage(); $str = getStrStorage();

$str->set("Foobar"); // No error $int->set("World"); // Error }

main();

Page 47: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

Generics

Page 48: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

Questions?

Page 49: Hack in 60 seconds - nyholm.tech · Agenda • Background • My 60 seconds • Features • Questions • How could you get started?

How to get started?• Online tutorial

(http://hacklang.org/tutorial)

• Start coding on you machine (http://developer.happyr.com/start-coding-with-hack)

• Miles Johnson has created Titon, “a fullstack Hack framework”. (https://github.com/titon/framework)

• Nuclide IDE(http://nuclide.io/)