Slide 1

Slide 1 text

HHVM/HackͰಘΔ໰୊ղܾྗ yuuki takezawa builderscon 2018

Slide 2

Slide 2 text

Profile • ஛ᖒ ༗و / ytake • גࣜձࣾΞΠελΠϧ CTO • PHP, Hack, Go, Scala • Apache Hadoop, Apache Spark, Apache Kafka


Slide 3

Slide 3 text

ͱΓ͋͛Δ͜ͱ • ༷ʑͳ໰୊ղܾ • HackʹΑΔ໰୊ղܾ • ΞϓϦέʔγϣϯʹ͓͚Δ໰୊ղܾ

Slide 4

Slide 4 text

໰୊ղܾͱ͸ • ໰୊Λղܾ͢Δɺ
 ͢ͳΘͪղΛൃݟ͢Δ͜ͱͰ͋Γɺ
 ࢥߟͷҰ෦෼Ͱ͋Δ • νʔϜʹ͓͚ΔࢥߟͰ͋ͬͨΓ
 ΦϒδΣΫτࢦ޲Ͱ͋ͬͨΓ

Slide 5

Slide 5 text

ͳͥHackΛ࢖͏ͷ͔

Slide 6

Slide 6 text

എܠ • શମతʹ͸PHPϝΠϯͷٕज़ελοΫ • WebΞϓϦέʔγϣϯ͕ϝΠϯ • Go੡ͷΞϓϦέʔγϣϯ΋͍͔ͭ͘ • ෼ੳॲཧ͸Scala੡͕͍͔ͭ͘

Slide 7

Slide 7 text

ݒ೦ • ͦΕ XX Ͱ΋Ͱ͖ΔΑ
 • ಛఆͷݴޠ͔͠஌Βͳ͍ • ։ൃͱ͍͏ߦҝʹΫϦΤΠςΟϒ͕͞ײ͡ΒΕͳ͍

Slide 8

Slide 8 text

બ୒ͷύϥυοΫε https://www.ted.com/talks/barry_schwartz_on_the_paradox_of_choice?language=ja

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

ΞϓϦέʔγϣϯ͸ ϦϦʔε͢Δ·ͰͰ͸ͳ͘ɺ ϦϦʔε͔ͯ͠Β͕ຊ൪

Slide 12

Slide 12 text

։ൃऀ͸ ΞϓϦέʔγϣϯͱͱ΋ʹ ੒௕͢Δ

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

։ൃͷଊ͑ํ • ͍ͭ΋ͷࢹ఺ͱগ͠ҧ͏ࢹ఺Λ௥Ճͯ͠ΈΔ • ҧ͏ࢹ఺ͷ͋ͱɺ͍ͭ΋ͷݴޠ͔Β • ૒ํ޲ʹಇ͘ݟ͑ํΛཆ͏͜ͱ͸ѱ͍͜ͱʁ • ͍ͭ΋ͷࢹ఺͚ͩɺ͸ѱ͍͜ͱʁ

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

ࢹ఺Λม͑Δํ๏ͷҰͭͱͯ͠ HackΛར༻

Slide 18

Slide 18 text

PHP͕ ͪΐͬͱૣ͘ͳͬͨ΍ͭͰ͠ΐʁ

Slide 19

Slide 19 text

HHVM/Hack • ݴޠͷϕʔεʹ͋Δͷ͸PHPͷվળ • I/O͕ൃੜ͢Δ΋ͷʹରͯ͠ͷAsync/Await
 શͯʹରͯ͠࡞༻͢ΔΘ͚Ͱ͸ͳ͍ͷͰɺෑډ͕௿͍
 • ݫ֨ͳܕνΣοΫͱίϨΫγϣϯɾ੬ऑੑରࡦ

Slide 20

Slide 20 text

for Developer • Atom + Nuclide • Visual Studio Code + Hack plugin • Docker (hhvm/hhvm)

Slide 21

Slide 21 text

.hhconfig

Slide 22

Slide 22 text

.hhconfig ͷجຊ • HackͰ࣮ߦ؀ڥʹઃஔ͢ΔϑΝΠϧ • ༷ʑͳઃఆΛهड़Ͱ͖Δ • PHPར༻Λ૝ఆ͠ͳ͍(PHPࠞࡏෆՄ) 
 assume_php = false(default: true)
 • Type Checker Ұ෦ແࢹ
 ignored_paths = [ "vendor/hhvm/hhast/.+" ]

Slide 23

Slide 23 text

.hhconfig ͋Ε͜Ε • λΠϓνΣοΧʔͷϝϞϦૢ࡞
 • ࢀর౉͠࢖͏ͳϞʔυ΋௥Ճ(3.28)
 disallow_return_by_ref
 disallow_array_cell_pass_by_ref

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

ۤ࿑ • IDEʹ͓͚Δิ׬ػೳͷಈ͖
 • Τϥʔ಺༰͕Θ͔Βͳ͍ • PHPʹݟ͍͑ͯͯผ෺ͱ͍͏ೝࣝΛ΋ͭ·Ͱ • ৘ใऩूྗ

Slide 26

Slide 26 text

Type Checker

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Type Checker • ίϯύΠϥϥΠΫʹಈ͘ܕνΣοΫπʔϧ • Ϟʔυ͸3ͭ Partial / Strict / Decl 
 • σϑΥϧτͰ͸Partial

Slide 29

Slide 29 text

Type Checker: Partial • PHPͷܕએݴ strictͱಉఔ౓ • ඞཁҎ্ʹܕνΣοΫ͸͠ͳ͍ • ओʹPHPͷίʔυΛ Hackͱ࣮ͯ͠ߦ͢Δ
 ίʔυҠ২தͷϑΝΠϧ౳Ͱར༻ • ࢀর౉͠ ར༻Մೳ

Slide 30

Slide 30 text

Type Checker: Decl •

Slide 31

Slide 31 text

Type Checker: Strict •

Slide 32

Slide 32 text

for Example • ίϯετϥΫλͰॳظԽ͍ͯ͠ͳ͍ίʔυ͸ܯࠂ • isset͡Όͳͯ͘ɺarray_key_existsΛ࢖͍ͳ͍͞ • ͨͩͷarray͡ΌΘ͔Βͳ͍ Vector͔Map࢖͍ͳ͍͞

Slide 33

Slide 33 text

ߟ͑ํΛม͑Δ • ͱΓ͋͑ͣarray
 ͦͷarray͸ͲΜͳ໾ׂ͕͋ΓɺͲ͏͍͏΋ͷͳͷ͔ • arrayͰ͋Δඞཁ͕͋Δͷ͔Ͳ͏͔

Slide 34

Slide 34 text

Hackͷarray͸ࡾछྨ • array
 ௨ৗͷ഑ྻ • varray
 ஋ͷΈͰߏ੒͞ΕΔ഑ྻ • darray
 σΟΫγϣφϦͷ഑ྻ

Slide 35

Slide 35 text

$varray = varray[ 'php', 'hack' ]; protected darray $darray = darray[ 'testing' => 'testing', 1 => 'testing' ]; public function failedVArray(): varray { return $this->varray; } public function getDArray(): darray { return $this->darray; } } ܕҧ͍ ໭Γܕҧ͍ ໭Γܕҧ͍

Slide 36

Slide 36 text

ΞϓϦέʔγϣϯʹ͓͚Δ ໰୊ղܾ

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

ܧঝΛ੍ޚ͢Δ • Sealed Class • Sealed Interface • Hack 3.27 Ҏ߱

Slide 39

Slide 39 text

<<__Sealed(Hoge::class)>> class SealedClass { } <<__Sealed(Sample::class)>> interface SealedInterface { } ࢦఆͨ͠ΫϥεҎ֎ ܧঝෆՄ ࢦఆͨ͠ΫϥεҎ֎ ܧঝෆՄɾ࣮૷ෆՄ

Slide 40

Slide 40 text

ܧঝʹ͍ͭͯͷߟ͑ํ • final classͷΈͰ੍໿(ڐՄ)Λ͔͚Δ͔
 • GenericsͰ஋දݱͷΈڐՄ͢Δͷ͔
 • ͦͷޙͷΞϓϦέʔγϣϯͷ੒௕ͱɺ
 ֦ுੑͰେ͖͘Ξϓϩʔν͕ҟͳΔ

Slide 41

Slide 41 text

ܕม׵

Slide 42

Slide 42 text

public function sum(): int { return 10 + "5e2"; }

Slide 43

Slide 43 text

public function sum(): int { return 10 + "5e2"; } Typing error This is a num (int/float) because this is used in an arithmetic operation. It is incompatible with a string.

Slide 44

Slide 44 text

ܕม׵: mixed

Slide 45

Slide 45 text

public function get($id): mixed { return //Կ͔Λฦ٫͢Δ; } $container = new Container(); $container->get('something');

Slide 46

Slide 46 text

private function invariantLoggerInterface( Container $container, ): LoggerInterface { $logger = $container ->get(LoggerInterface::class); invariant( $logger instanceof LoggerInterface, "Interface '\Psr\Log\LoggerInterface' is not implemented by this class", ); return $logger; } mixedͷ৔߹͸Կ͕ฦ٫͞ΕΔ͔Θ͔Βͳ͍ ظ଴஋ͷ΋ͷ͕ฦ٫͞ΕΔ͔Ͳ͏͔ ඞͣهड़ͯ͠ɺTypeChecker޲͚ʹهड़

Slide 47

Slide 47 text

final class Util { public function something(mixed $any): mixed { if($any is int) { // } if($any is string) { // } } }

Slide 48

Slide 48 text

Type Constants

Slide 49

Slide 49 text

interface TypeInterface { abstract const type T; public function getNative(): this::T; }

Slide 50

Slide 50 text

class UserType implements TypeInterface { const type T = Vector; public function getNative(): this::T { return new Vector([1,2]); } }

Slide 51

Slide 51 text

Factory

Slide 52

Slide 52 text

class Sample { }

Slide 53

Slide 53 text

final class Factory { protected array $array = [ 'Sample' => Sample::class ]; public function get(string $id) { $key = \ucfirst(\strtolower($id)); if(\array_key_exists($key, $this->array)) { $class = $this->array[$key]; return new $class(); } } } class_existsͳͲ΋

Slide 54

Slide 54 text

<<__ConsistentConstruct>> class Sample { } constructor੍ޚ

Slide 55

Slide 55 text

final class Factory { protected Map> $map = Map{ 'Sample' => Sample::class }; public function get(string $id): Sample { $class = $this->map->get(\ucfirst(\strtolower($id))); if (!\is_null($class)) { return new $class(); } throw new \RuntimeException(); } } classจࣈྻࢦఆ ࣮֬ͳΠϯελϯεੜ੒

Slide 56

Slide 56 text

ࣝผͷҧ͍

Slide 57

Slide 57 text

final class BookId { private $id; public function __construct(string $id) { $this->id = $id; } public function getValue(): string { return $this->id; } }

Slide 58

Slide 58 text

final class BookId { public function __construct( private string $id ) {} public function getValue(): string { return $this->id; } }

Slide 59

Slide 59 text

ҧ͏දݱํ๏

Slide 60

Slide 60 text

abstract class Identifier { public function __construct( protected T $id ) {} public function getValue(): T { return $this->id; } } final class BookId extends Identifier { } Generics

Slide 61

Slide 61 text

ॻ੶දݱํ๏

Slide 62

Slide 62 text

class Book { private $id; private $title; private $price; public function __construct( BookId $id, BookTitle $title, Price $price ) { $this->id = $id; $this->title = $title; $this->price = $price; } public function getId(): BookId { return $this->id; } // লུ }

Slide 63

Slide 63 text

class Book { public function __construct( private BookId $id, private BookTitle $title, private Price $price ) {} public function getId(): BookId { return $this->id; } // লུ } ݎ੍͍໿ ݎ੍͍໿

Slide 64

Slide 64 text

object(Book)#2 (3) { ["id":"Book":private]=> object(BookId)#3 (1) { ["id":protected]=> string(36) "6ad4bb95-262c-4a5b-a6c7-ac9b5cadf707" } ["title":"Book":private]=> string(18) "HHVM/Hack Practice" ["price":"Book":private]=> int(2999) }

Slide 65

Slide 65 text

ॻ੶ίϨΫγϣϯදݱํ๏

Slide 66

Slide 66 text

class BookCollection { protected $books = []; public function __construct(array $books = []) { $this->books = $books; } public function toArray(): array { $books = []; foreach($this->books as $book) { $books[] = new Book( new BookId($book['id']), new BookTitle($book['title']), new Price($book['price']) ); } return $books; } } ϑΟʔϧυ੍໿ͳ͠

Slide 67

Slide 67 text

type BookShape = shape( 'book_id' => string, 'title' => string, 'price' => int ); ϑΟʔϧυ੍໿

Slide 68

Slide 68 text

class BookCollection { protected Vector $v = Vector{ }; public function __construct( protected varray $array ) { $this->v = $this->vec(); } protected function vec(): Vector { $v = Vector{ }; foreach($this->array as $row) { $v->add(new Book( new BookId($row['book_id']), new BookTitle($row['title']), new Price($row['price']) )); } return $v; } public function toArray(): varray { return $this->v->toVArray(); } } shapeΛvarrayͰ to Vector

Slide 69

Slide 69 text

$v = new Vector([ new Book('1234', 'testing', 2999), new Book('1235', 'testing', 2999), ]); $v = $v->filter( ($t) ==> $t->getId() === '1234' )->immutable(); collection filter Πϛϡʔλϒϧʹ

Slide 70

Slide 70 text

ෳ਺ҙຯΛ࣋ͨͤͳ͍ͨΊͷ੍ޚ • ౎߹ͷ͍͍ΫϥεΛ࡞Βͳ͍
 • ू໿ΛΑΓߟ͑Δ
 • ͳΜͰ΋Ͱ͖ΔΫϥεͰ͸ͳ͍
 ͜Ε͔͠Ͱ͖ͳ͍Ϋϥε

Slide 71

Slide 71 text

ݫ֨ͳܕΛPHPΞϓϦέʔγϣϯʹద༻͢Δҙຯ • ͱΓ͋͑ͣಈ͘ঢ়ଶʹͰ͖Ε͹͍͍ • ࢓༷มߋ͕͋Ε͹ܕ͕ͳ͍ํ͕ଟ༷ੑ͋ΔΜ͡Όͳ͍ʁ • ͦ΋ͦ΋ଞͷݴޠͰΑ͘ͳ͍ʁ

Slide 72

Slide 72 text

ͱΓ͋͑ͣಈ͘ঢ়ଶ͔Β
 ៉ྷʹ͢Δ࣌ؒ͸ͳ͔ͳ͔དྷͳ͍

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

બ୒ࢶͱࢹ໺Λ޿͛Δ

Slide 75

Slide 75 text

໰୊Λղܾ͢Δɺ
 ͢ͳΘͪղΛൃݟ͢Δ͜ͱͰ͋Γɺ
 ࢥߟͷҰ෦෼

Slide 76

Slide 76 text

ղܾ͢ΔͨΊͷྗͱ ࢹ໺