Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Hack the Future

550 vues

Publié le

Talk given at PHP World 2015 about the Hack language released by Facebook. A short history and look at it's key features as well as how Hack and PHP are evolving together.

Publié dans : Internet
  • Soyez le premier à commenter

  • Soyez le premier à aimer ceci

Hack the Future

  1. 1. Hack the Future Jason McCreary "JMac" @gonedark
  2. 2. Hack the Future - Jason McCreary - php[world] 2015 Talk the Talk 1. What is Hack? 2. Key Features of Hack 3. Unsupported PHP features in Hack 4. How can I use Hack? 5. Why Hack? 6. The Future 2
  3. 3. Hack the Future - Jason McCreary - php[world] 2015 But first… A quick disclaimer. 3
  4. 4. Hack the Future - Jason McCreary - php[world] 2015 "Some things are going to be said." 4
  5. 5. Hack the Future - Jason McCreary - php[world] 2015 "This is not a talk to push Hack." 5
  6. 6. Hack the Future - Jason McCreary - php[world] 2015 "This is not a talk to shit on PHP." 6
  7. 7. Hack the Future - Jason McCreary - php[world] 2015 "This is a talk about the evolution of a language." 7
  8. 8. Hack the Future - Jason McCreary - php[world] 2015 What is Hack? A very brief history. 8
  9. 9. Hack the Future - Jason McCreary - php[world] 2015 Brief History • In 2009, Facebook released HipHop - a PHP compiler • In 2010, Facebook introduced new features into PHP • In 2012, Facebook began working on Hack • In 2013, Facebook released HHVM - a HipHopVM • In 2014, Facebook released Hack 9
  10. 10. Hack the Future - Jason McCreary - php[world] 2015 So what is Hack? "Hack is a programming language for HHVM that interoperates seamlessly with PHP." 10
  11. 11. Hack the Future - Jason McCreary - php[world] 2015 So what is Hack, really? "Hack is a superset of PHP that runs on HHVM which provides type checking." 11
  12. 12. Hack the Future - Jason McCreary - php[world] 2015 WTF Facebook? Just use PHP. 12
  13. 13. Hack the Future - Jason McCreary - php[world] 2015 "Well, they do." 13
  14. 14. Hack the Future - Jason McCreary - php[world] 2015 "Facebook has millions of lines of PHP." 14
  15. 15. Hack the Future - Jason McCreary - php[world] 2015 "Instead of a rewrite or waiting or influencing the PHP community, they made Hack." 15
  16. 16. Hack the Future - Jason McCreary - php[world] 2015 "The goal being to write cleaner, safer, and refactorable code." 16
  17. 17. Hack the Future - Jason McCreary - php[world] 2015 Key Features A deeper look at some of the features of Hack. 17
  18. 18. Hack the Future - Jason McCreary - php[world] 2015 Key Features in Hack • Type Annotations • Additional types • Generics and Nullable • Asynchronous programming • Syntactic sugar 18
  19. 19. Hack the Future - Jason McCreary - php[world] 2015 Type Annotations <?hh
 // parameter type annotations function output(string $str) { echo 'Hello, ' . $str; } // return type annotations function get_output(string $str): string { return 'Hello, ' . $str; } 19
  20. 20. Hack the Future - Jason McCreary - php[world] 2015 "Hack is not a statically typed language." 20
  21. 21. Hack the Future - Jason McCreary - php[world] 2015 "Hack allows more type hinting than PHP." 21
  22. 22. Hack the Future - Jason McCreary - php[world] 2015 "HHVM performs type checking before runtime which ensures your code is type safe." 22
  23. 23. Hack the Future - Jason McCreary - php[world] 2015 Additional Types • Collections • Enums • Shapes • Tuples • Custom Types 23
  24. 24. Hack the Future - Jason McCreary - php[world] 2015 The problem with array "In PHP, arrays are used for all the things!" 24
  25. 25. Hack the Future - Jason McCreary - php[world] 201525
  26. 26. Hack the Future - Jason McCreary - php[world] 2015 The problem with array <?php // indexed, starting at 0 $arr1 = array(1, 2, 3); // indexed, starting at 1 $arr2 = array(1 => 1, 2, 3); // keyed, mix of integer keys and string keys $arr3 = array("foo" => 1, 73 => 2, "bar" => 3); // all array values are the same 26
  27. 27. Hack the Future - Jason McCreary - php[world] 2015 The problem with array "Primitive obsession is a code smell!" 27
  28. 28. Hack the Future - Jason McCreary - php[world] 2015 The problem with array "In PHP, arrays are primitives (i.e. non-objects)." 28
  29. 29. Hack the Future - Jason McCreary - php[world] 2015 The problem with array $ php -r 'var_dump(is_object(array()));' bool(false) 29
  30. 30. Hack the Future - Jason McCreary - php[world] 2015 Collections • Vector - an ordered, index collection • Map - a key-based collection • Set - an unordered, uniquely value collection • Pair - an immutable, indexed, 2 element collection • Both immutable and mutable versions 30
  31. 31. Hack the Future - Jason McCreary - php[world] 2015 Collections <?hh 
 $vector = Vector {5, 10, 15}; $map = Map {"A" => 1, "B" => 2, "C" => 3}; $set = Set {"A", "B"}; 31
  32. 32. Hack the Future - Jason McCreary - php[world] 2015 Enums <?hh enum DayOfWeek: int { Sunday = 0; Monday = 1; Tuesday = 2; Wednesday = 3; Thursday = 4; Friday = 5; Saturday = 6; } echo DayOfWeek::Wednesday; // outputs 3 32
  33. 33. Hack the Future - Jason McCreary - php[world] 2015 Shapes <?hh // fixed size and type // keyed collection of mixed values $sh = shape('x' => 3, 'y' => 5); echo $sh['x'] . ',' . $sh['y']; // outputs 3,5 33
  34. 34. Hack the Future - Jason McCreary - php[world] 2015 Tuples <?hh // fixed size and type, index array of mixed values $tup = tuple('3', 2, 3, 4, 'hi'); echo $tup[1]; // outputs 2 unset($tup[0]); // error $tup[5] = 'new value'; // error $tup[4] = 'change value'; // ok, string to string 34
  35. 35. Hack the Future - Jason McCreary - php[world] 2015 Custom Types <?hh
 // define your own types type MyInt = int; type Point = (int, int); 35
  36. 36. Hack the Future - Jason McCreary - php[world] 2015 Generics and Nullable • Generics - ability to parameterize the type of a class or method and declare it upon instantiation • Nullable - ability to declare a type allows null 36
  37. 37. Hack the Future - Jason McCreary - php[world] 2015 Generics <?hh class Box<T> { protected T $data; public function __construct(T $data) { $this->data = $data; } public function getData(): T { return $this->data; } } 37
  38. 38. Hack the Future - Jason McCreary - php[world] 2015 Nullable <?hh function check_not_null(?int $x): int { if ($x === null) { return -1; } else { return $x; } } 38
  39. 39. Hack the Future - Jason McCreary - php[world] 2015 Asynchronous Programming <?hh async function helloAfter($name, $timeout): Awaitable<string> { await SleepWaitHandle::create($timeout * 1000000); echo sprintf("hello %sn", $name); } async function run() { $joe = helloAfter('Joe', 3); $mike = helloAfter('Mike', 1); await GenArrayWaitHandle::create(array($joe, $mike)); } run()->join(); // prints: // hello Mike // hello Joe 39
  40. 40. Hack the Future - Jason McCreary - php[world] 2015 Syntactic Sugar • Constructor Argument Promotion • Lambdas 40
  41. 41. Hack the Future - Jason McCreary - php[world] 2015 Constructor Argument Promotion <?php class Person { private string $name; private int $age; public function __construct(string $name, int $age) { $this->name = $name; $this->age = $age; } } 41
  42. 42. Hack the Future - Jason McCreary - php[world] 2015 Constructor Argument Promotion <?hh class Person { public function __construct(public string $name, protected int $age) {} } 42
  43. 43. Hack the Future - Jason McCreary - php[world] 2015 Lambdas <?php $foo = function($x) { return $x + 1; } $foo(12); // returns 13 $squared = array_map(function($x) { return $x * $x; }, array(1,2,3)); var_dump($squared); // $squared is array(1,4,9) 43
  44. 44. Hack the Future - Jason McCreary - php[world] 2015 Lambdas <?hh $foo = $x ==> $x + 1; $foo(12); // returns 13 $squared = array_map($x ==> $x * $x, array(1,2,3)); var_dump($squared); // $squared is array(1,4,9) 44
  45. 45. Hack the Future - Jason McCreary - php[world] 2015 Unsupported PHP Features The things Hack no likey. 45
  46. 46. Hack the Future - Jason McCreary - php[world] 2015 Unsupported PHP Features • Alternative syntaxes: AND, OR, XOR, endif, etc. • Dynamic features: eval, continue n, break n,
 $$var, $obj->{$var} • Top Level Code • References • Globals • Case insensitive function calls • Explicitly named constructors 46
  47. 47. Hack the Future - Jason McCreary - php[world] 2015 How can I use Hack? What you need to do to start Hack’in. 47
  48. 48. Hack the Future - Jason McCreary - php[world] 2015 "Change <?php to <?hh" 48
  49. 49. Hack the Future - Jason McCreary - php[world] 2015 "You’re right, first install HHVM." 49
  50. 50. Hack the Future - Jason McCreary - php[world] 2015 "Then change <?php to <?hh!" 50
  51. 51. Hack the Future - Jason McCreary - php[world] 2015 Hack Modes • Hack has three modes: strict, partial, and decl • strict - type checker will catch every type error • partial - type checker checks code that uses types • decl - allows strict code to call separate, legacy code • The default is partial 51
  52. 52. Hack the Future - Jason McCreary - php[world] 2015 Hack Modes <?hh // strict function example(int $x) { echo "I'm so strict!"; } 52
  53. 53. Hack the Future - Jason McCreary - php[world] 2015 Why Hack? Some reasons for using Hack. 53
  54. 54. Hack the Future - Jason McCreary - php[world] 2015 Reasons to Hack • Code clarity • Performance • Language features, like Asynchronous Programming • Evolution 54
  55. 55. Hack the Future - Jason McCreary - php[world] 2015 The Future So bright, I gotta wear shades. 55
  56. 56. Hack the Future - Jason McCreary - php[world] 2015 Some things in PHP • Variadic functions (PHP 5.6) • Scalar type declarations (PHP 7) • Return type declarations (PHP 7) • Null coalesce operator (PHP 7) • Deprecations: alternative syntax and named constructors (PHP 7) 56
  57. 57. Hack the Future - Jason McCreary - php[world] 2015 Look familiar? <?php function add(int $x, int $y): int { return $x + $y; } $nil = null; $foo = $nil ?? add(2, 3); 57
  58. 58. Hack the Future - Jason McCreary - php[world] 2015 "Yeah, Hack had it first." 58
  59. 59. Hack the Future - Jason McCreary - php[world] 2015 "Hack, coming soon in PHP 7+." 59
  60. 60. Hack the Future - Jason McCreary - php[world] 2015 "This is evolution." 60
  61. 61. Hack the Future - Jason McCreary - php[world] 2015 Questions… Ask now or tweet me @gonedark. 61

×