Upload
jason-mccreary
View
430
Download
0
Embed Size (px)
Citation preview
Hack the FutureJason McCreary "JMac" @gonedark
Hack the Future - Jason McCreary - php[world] 2015
Talk the Talk
1. What is Hack?2. Key Features of Hack3. Unsupported PHP features in Hack4. How can I use Hack?5. Why Hack?6. The Future
2
Hack the Future - Jason McCreary - php[world] 2015
But first…A quick disclaimer.
3
Hack the Future - Jason McCreary - php[world] 2015
"Some things are going to be said."
4
Hack the Future - Jason McCreary - php[world] 2015
"This is not a talk to push Hack."
5
Hack the Future - Jason McCreary - php[world] 2015
"This is not a talk to shit on PHP."
6
Hack the Future - Jason McCreary - php[world] 2015
"This is a talk about the evolution of a language."
7
Hack the Future - Jason McCreary - php[world] 2015
What is Hack?A very brief history.
8
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 HipHop VM• In 2014, Facebook released Hack
9
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
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
Hack the Future - Jason McCreary - php[world] 2015
WTF Facebook?Just use PHP.
12
Hack the Future - Jason McCreary - php[world] 2015
"Well, they do."
13
Hack the Future - Jason McCreary - php[world] 2015
"Facebook has millions of lines of PHP."
14
Hack the Future - Jason McCreary - php[world] 2015
"Instead of a rewrite or waiting or influencing the PHP community, they made Hack."
15
Hack the Future - Jason McCreary - php[world] 2015
"The goal being to write cleaner, safer, andrefactorable code."
16
Hack the Future - Jason McCreary - php[world] 2015
Key FeaturesA deeper look at some of the features of Hack.
17
Hack the Future - Jason McCreary - php[world] 2015
Key Features in Hack
• Type Annotations• Additional types• Generics and Nullable• Asynchronous programming• Syntactic sugar
18
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
Hack the Future - Jason McCreary - php[world] 2015
"Hack is not a statically typed language."
20
Hack the Future - Jason McCreary - php[world] 2015
"Hack allows more type hinting than PHP."
21
Hack the Future - Jason McCreary - php[world] 2015
"HHVM performs type checking before runtime which ensures your code is type safe."
22
Hack the Future - Jason McCreary - php[world] 2015
Additional Types
• Collections• Enums• Shapes• Tuples• Custom Types
23
Hack the Future - Jason McCreary - php[world] 2015
The problem with array
"In PHP, arrays are used for all the things!"
24
Hack the Future - Jason McCreary - php[world] 201525
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
Hack the Future - Jason McCreary - php[world] 2015
The problem with array
"Primitive obsession is a code smell!"
27
Hack the Future - Jason McCreary - php[world] 2015
The problem with array
"In PHP, arrays are primitives (i.e. non-objects)."
28
Hack the Future - Jason McCreary - php[world] 2015
The problem with array
$ php -r 'var_dump(is_object(array()));'
bool(false)
29
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
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
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
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
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
Hack the Future - Jason McCreary - php[world] 2015
Custom Types
<?hh // define your own types
type MyInt = int;
type Point = (int, int);
35
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
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
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
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 %s\n", $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
Hack the Future - Jason McCreary - php[world] 2015
Syntactic Sugar
• Constructor Argument Promotion• Lambdas
40
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
Hack the Future - Jason McCreary - php[world] 2015
Constructor Argument Promotion
<?hh
class Person {
public function __construct(public string $name,
protected int $age) {}
}
42
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
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
Hack the Future - Jason McCreary - php[world] 2015
Unsupported PHP FeaturesThe things Hack no likey.
45
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
Hack the Future - Jason McCreary - php[world] 2015
How can I use Hack?What you need to do to start Hack’in.
47
Hack the Future - Jason McCreary - php[world] 2015
"Change <?php to <?hh"
48
Hack the Future - Jason McCreary - php[world] 2015
"You’re right, first install HHVM."
49
Hack the Future - Jason McCreary - php[world] 2015
"Then change <?php to <?hh!"
50
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
Hack the Future - Jason McCreary - php[world] 2015
Hack Modes
<?hh // strict
function example(int $x) {
echo "I'm so strict!";
}
52
Hack the Future - Jason McCreary - php[world] 2015
Why Hack?Some reasons for using Hack.
53
Hack the Future - Jason McCreary - php[world] 2015
Reasons to Hack
• Code clarity• Performance• Language features, like Asynchronous Programming• Evolution
54
Hack the Future - Jason McCreary - php[world] 2015
The FutureSo bright, I gotta wear shades.
55
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
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
Hack the Future - Jason McCreary - php[world] 2015
"Yeah, Hack had it first."
58
Hack the Future - Jason McCreary - php[world] 2015
"Hack, coming soon in PHP 7+."
59
Hack the Future - Jason McCreary - php[world] 2015
"This is evolution."
60
Hack the Future - Jason McCreary - php[world] 2015
Questions…Ask now or tweet me @gonedark.
61