Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Hack in 60 seconds
Tobias Nyholm
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
Facebook had a problem
HipHop
• HPHPc
• HPHPi & HPHPd
• JIT
• HHVM
FastCGI mode
• Use as PHP-FPM
• Linux packages
• http://hhvm.com/blog/1817/fastercgi-with-hhvm
HPHPc > HPHPi > JIT > HHVM ...
FastCGI mode
HPHPc > HPHPi > JIT > HHVM > FastCGI
PHP 5.5 HHVM
Request per second 23 184
Time per request 43 ms 5 ms
FastCGI mode
HPHPc > HPHPi > JIT > HHVM > FastCGI
PHP 5.5 HHVM
FIB(5) 13 789 8 842
FIB(15) 3 202 8 892
FIB(25) 119 5 581
F...
Your application will run about 5 times faster
Wikipedia
http://hhvm.com/blog/7205/wikipedia-on-hhvm
Wikipedia
http://hhvm.com/blog/7205/wikipedia-on-hhvm
What about PHP7?
What about Hack?
123 == “123foo” "6" == " 6"
"133" == "0133"
133 ! = 0133
“025” == 031NULL < -1
NULL == 0
“foo” == TRUE
“foo” == 0
TRUE != ...
Hack
• Backwards compatible with PHP
• Slightly faster on HHVM
• Syntactic sugar
• Gradually typed
Static typed languages
• C, Go, Java, Haskell
public class Foobar{
String foo;
int bar;
String baz(int i){
return “Biz”;
}...
Dynamic typed language
• PHP, Ruby, Python, Javascript
class Storage {
private $foo;
String set($i) {
$this->foo=$i
}
}
$s...
But what if…
$storage = new Storage();
$storage->set(4711);
// …
$array = $storage->get();
echo $array[0];
// …
$user = $s...
Hack modes
• Strict - Type check everything
• Partial - Check whatever is annotated
• Decl - Check nothing
Hack modes
<?hh //strict
–The audience
“Show some code”
<?php
class MessageProvider {
private $counter=0;
private $message=“”;
public function set($text) {
$this->message = $text...
<?hh
class MessageProvider {
private $counter=0;
private $message=“”;
public function set($text) {
$this->message = $text;...
<?hh
class MessageProvider {
private int $counter=0;
private string $message=“”;
public function set($text) {
$this->messa...
<?hh
class MessageProvider {
private int $counter=0;
private string $message=“”;
public function set(string $text) {
$this...
<?hh
class MessageProvider {
private int $counter=0;
private string $message=“”;
public function set(string $text): int {
...
<?hh
function main() {
$mp= new MessageProvider();
$var = $mp->set(“hello”);
$mp->set($var);
}
main();
/index.php:6:14,17:...
Syntactic sugar
<?hh
class MessageProvider {
private int $counter=0;
private string $message=“”;
public function __constru...
Syntactic sugar
<?hh
class MessageProvider {
public function __construct(
private int $counter,
private string $message
) ...
Syntactic sugar
//$arr = array('foo', 'hello', 'bazbar');
Syntactic sugar
<?php
$arr = array('hello', 'bazbar', 'foo');
usort($arr, functi...
<?php
$input = array(2, 3, 5, 6, 7);
$factor = 3;
$result = array_filter(
array_map(
function($a) use ($factor) {
return $a...
<?hh
$input = array(2, 3, 5, 6, 7);
$factor = 3;
$result = array_filter(
array_map($a ==> $a * $factor, $input),
$a ==> $a ...
Traits
trait Foo {
public function bar() {
return $this->biz();
}
}
class Baz {
use Foo;
public function biz() {}
}
Traits with requirements
trait Foo {
require extends A;
require implements B;
public function bar() {
return $this->biz();...
Nullable
<?hh
function foo(int $x): int {
return $x + 4711;
}
function main() {
$y = foo(null);
}
main();
Nullable
<?hh
function foo(int $x): int {
return $x + 4711;
}
function main() {
$y = foo(null);
}
main();
Arrays are great!
$vector = [4,7,1,1];
$map = array(‘foo’=>47, ‘bar’=>11);
$set = array_unique(array(4,7,1,1));
$object = ...
Shape
type Heading = shape(
'nr' => int,
'full' => string,
'content' => string,
'attributes' => string,
);
Generics
class Storage<T> {
protected T $var;
public function __construct(T $var) {
$this->var = $var;
}
public function g...
Generics
Questions?
How to get started?
• Online tutorial 

(http://hacklang.org/tutorial)
• Start coding on you machine 

(http://developer.h...
Hack in 60 seconds NYC
Hack in 60 seconds NYC
Hack in 60 seconds NYC
Hack in 60 seconds NYC
Upcoming SlideShare
Loading in …5
×

Hack in 60 seconds NYC

843 views

Published on

An introduction to Hack. I did this talk in July 2015 in New York.

Published in: Data & Analytics
  • Be the first to comment

Hack in 60 seconds NYC

  1. 1. Hack in 60 seconds Tobias Nyholm
  2. 2. Agenda • Background • My 60 seconds • Features • Questions • How could you get started?
  3. 3. About me • Tobias Nyholm, @tobiasnyholm • Happyr • Certified Symfony developer • Co-host of Sound of Symfony podcast
  4. 4. Facebook had a problem
  5. 5. HipHop • HPHPc • HPHPi & HPHPd • JIT • HHVM
  6. 6. FastCGI mode • Use as PHP-FPM • Linux packages • http://hhvm.com/blog/1817/fastercgi-with-hhvm HPHPc > HPHPi > JIT > HHVM > FastCGI
  7. 7. FastCGI mode HPHPc > HPHPi > JIT > HHVM > FastCGI PHP 5.5 HHVM Request per second 23 184 Time per request 43 ms 5 ms
  8. 8. FastCGI mode HPHPc > HPHPi > JIT > HHVM > FastCGI 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)
  9. 9. Your application will run about 5 times faster
  10. 10. Wikipedia http://hhvm.com/blog/7205/wikipedia-on-hhvm
  11. 11. Wikipedia http://hhvm.com/blog/7205/wikipedia-on-hhvm
  12. 12. What about PHP7?
  13. 13. What about Hack?
  14. 14. 123 == “123foo” "6" == " 6" "133" == "0133" 133 ! = 0133 “025” == 031NULL < -1 NULL == 0 “foo” == TRUE “foo” == 0 TRUE != 0 “123” != “123foo”
  15. 15. Hack • Backwards compatible with PHP • Slightly faster on HHVM • Syntactic sugar • Gradually typed
  16. 16. 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! }
  17. 17. 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);
  18. 18. But what if… $storage = new Storage(); $storage->set(4711); // … $array = $storage->get(); echo $array[0]; // … $user = $storage->get(); echo $user->getUsername();
  19. 19. Hack modes • Strict - Type check everything • Partial - Check whatever is annotated • Decl - Check nothing
  20. 20. Hack modes <?hh //strict
  21. 21. –The audience “Show some code”
  22. 22. <?php class MessageProvider { private $counter=0; private $message=“”; public function set($text) { $this->message = $text; $this->counter++; return $this->counter; } }
  23. 23. <?hh class MessageProvider { private $counter=0; private $message=“”; public function set($text) { $this->message = $text; $this->counter++; return $this->counter; } }
  24. 24. <?hh class MessageProvider { private int $counter=0; private string $message=“”; public function set($text) { $this->message = $text; $this->counter++; return $this->counter; } }
  25. 25. <?hh class MessageProvider { private int $counter=0; private string $message=“”; public function set(string $text) { $this->message = $text; $this->counter++; return $this->counter; } }
  26. 26. <?hh class MessageProvider { private int $counter=0; private string $message=“”; public function set(string $text): int { $this->message = $text; $this->counter++; return $this->counter; } }
  27. 27. <?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
  28. 28. 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; } }
  29. 29. Syntactic sugar <?hh class MessageProvider { public function __construct( private int $counter, private string $message ) { } }
  30. 30. Syntactic sugar
  31. 31. //$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));
  32. 32. <?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);
  33. 33. <?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);
  34. 34. Traits trait Foo { public function bar() { return $this->biz(); } } class Baz { use Foo; public function biz() {} }
  35. 35. Traits with requirements trait Foo { require extends A; require implements B; public function bar() { return $this->biz(); } }
  36. 36. Nullable <?hh function foo(int $x): int { return $x + 4711; } function main() { $y = foo(null); } main();
  37. 37. Nullable <?hh function foo(int $x): int { return $x + 4711; } function main() { $y = foo(null); } main();
  38. 38. 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’];
  39. 39. Shape type Heading = shape( 'nr' => int, 'full' => string, 'content' => string, 'attributes' => string, );
  40. 40. Generics class 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();
  41. 41. Generics
  42. 42. Questions?
  43. 43. 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/)

×