SlideShare a Scribd company logo
1 of 85
Download to read offline
Hack Tutorial
吉澤和香奈
自己紹介
↳ 吉澤和香奈
↳ カツラじゃありません
↳ 1987/6/30生まれO型
↳ PHPに出会ったのは2012
年の夏です
↳ HNは「wakana」「わかな
だょ〜」「ブ〜バ〜」等、気
分により多数あります
Hackとは
Facebookが作った
PHPを拡張した
静的型検査する
プログラミング言語です
Facebookが作った
PHPを拡張した
静的型検査する
プログラミング言語です
→型に強いです
HHVMとは
PHP/Hackをバイナリコードに
変換させるVMです
(HipHop Virtual Machine)
PHP/Hackをバイナリコードに
変換させるVMです
(HipHop Virtual Machine)
→読み込みが早くなります
HHVMで
PHPと
Hackの
共存が実現できます!!
簡単に移行が実現できそうです
Tutorialやってみました
Hack Tutorial(公式)
http://hacklang.org/tutorial/
2015/1/26現在のものです
Exercise 1
/***************************************************/
/****************** Hack Tutorial ******************/
/***************************************************/
/************ Click 'Next' to get started! *********/
Exercise 2
<?php
// ^-- FIXME: replace <?php with <?hh
// A Hack file always starts with <?hh
<?hh
// ^-- FIXME: replace <?php with <?hh
// A Hack file always starts with <?hh
Exercise 3
<?hh
// Hack functions are annotated with types.
function my_negation(bool $x): bool {
return !$x;
}
// FIXME: annotate this function parameter
// and return with the type 'int'.
function add_one(/* TODO */ $x): /* TODO */ {
return $x+1;
}
<?hh
// Hack functions are annotated with types.
function my_negation(bool $x): bool {
return !$x;
}
// FIXME: annotate this function parameter
// and return with the type 'int'.
function add_one(int $x): int {
return $x+1;
}
Exercise 4
<?hh
/* Hack errors come in multiple parts.
* Hover over the underlined parts!
*/
function add_one(int $x): int {
return $x+1;
}
function test(): void {
$my_string = 'hello';
// Some clever code ...
add_one($my_string);
}
<?hh
/* Hack errors come in multiple parts.
* Hover over the underlined parts!
*/
function add_one(int $x): int {
return $x+1;
}
function test(): void {
$my_string = 'hello';
// Some clever code ...
add_one((int) $my_string);
}
Exercise 5
<?hh
// Prefixing a type with '?' permits null.
// TODO: fix the type of the parameter $x to permit null.
function f(int $x): void {
var_dump($x);
}
function test(): void {
f(123);
f(null);
}
<?hh
// Prefixing a type with '?' permits null.
// TODO: fix the type of the parameter $x to permit null.
function f(?int $x): void {
var_dump($x);
}
function test(): void {
f(123);
f(null);
}
Exercise 6
<?hh
interface User { public function getName(): string; }
function get_user_name(?User $user): string {
if($user !== null) {
// We checked that $user was not null.
// Its type is now 'User'.
/* TODO: return $user->getName() */
}
return '<invalid name>';
}
function test(User $user) {
$name1 = get_user_name($user);
$name2 = get_user_name(null);
}
<?hh
interface User { public function getName(): string; }
function get_user_name(?User $user): string {
if($user !== null) {
// We checked that $user was not null.
// Its type is now 'User'.
return $user->getName();
}
return '<invalid name>';
}
function test(User $user) {
$name1 = get_user_name($user);
$name2 = get_user_name(null);
}
Exercise 7
<?hh
interface User { public function getName(): string; }
// There are many ways to handle null values.
// Throwing an exception is one of them.
function get_user_name(?User $user): string {
if($user === null) {
throw new RuntimeException('Invalid user name');
}
/* TODO: return $user->getName() */
}
function test(User $user) {
$name1 = get_user_name($user);
$name2 = get_user_name(null);
}
<?hh
interface User { public function getName(): string; }
// There are many ways to handle null values.
// Throwing an exception is one of them.
function get_user_name(?User $user): string {
if($user === null) {
throw new RuntimeException('Invalid user name');
}
return $user->getName();
}
function test(User $user) {
$name1 = get_user_name($user);
$name2 = get_user_name(null);
}
Exercise 8
<?hh
// Hack introduces new collection types (Vector, Set and Map).
function test(): int {
// Vector is preferred over array(1, 2, 3)
$vector = Vector {1, 2, 3};
$sum = 0;
foreach ($vector as $val) {
$sum += $val;
}
return $sum;
}
<?hh
// Hack introduces new collection types (Vector, Set and Map).
function test(): int {
// Vector is preferred over array(1, 2, 3)
$vector = Vector {1, 2, 3};
$sum = 0;
foreach ($vector as $val) {
$sum += $val;
}
return $sum;
}
// Arrayよりも高速なVectorが使えます
Exercise 9
<?hh
// Hack uses generics for Collection types.
// TODO: fix the return type of the function 'test'
function test(): Vector<string> {
$vector = Vector {1, 2, 3};
return $vector;
}
<?hh
// Hack uses generics for Collection types.
// TODO: fix the return type of the function 'test'
function test(): Vector<string> {
$vector = Vector {“1”, “2”, “3”};
return $vector;
}
Exercise 10
<?hh
function vector_add1(Vector<int> $v): Vector<int> {
// Example of lambda expressions.
return $v->map($x ==> $x + 1);
}
function vector_mult2(Vector<int> $v): Vector<int> {
// TODO: write a function multiplying all the elements by 2
}
<?hh
function vector_add1(Vector<int> $v): Vector<int> {
// Example of lambda expressions.
return $v->map($x ==> $x + 1);
}
function vector_mult2(Vector<int> $v): Vector<int> {
// TODO: write a function multiplying all the elements by 2
return $v->map($x ==> $x * 2);
}
Exercise 11
/*
* Congratulations!
* You completed the beginner's tutorial.
*
* Click next to continue in expert mode.
*/
Exercise 12
<?hh
// All the members of a class must be initialized
class Point {
private float $x;
private float $y;
public function __construct(float $x, float $y) {
$this->x = $x;
// FIXME: initalize the member 'y'
}
}
<?hh
// All the members of a class must be initialized
class Point {
private float $x;
private float $y;
public function __construct(float $x, float $y) {
$this->x = $x;
// FIXME: initalize the member 'y'
$this->y = $y;
}
}
Exercise 13
<?hh
// Check out this new syntax!
// It's shorter and does the same thing ...
class Point {
public function __construct(
private float $x,
private float $y
) {}
}
<?hh
// Check out this new syntax!
// It's shorter and does the same thing ...
class Point {
public function __construct(
private float $x,
private float $y
) {}
}
// このような新しい書き方で今までより短くなります
Exercise 14
<?hh
// You can create your own generics!
class Store<T> {
public function __construct(private T $data) {}
public function get(): T { return $this->data; }
public function set(T $x): void { $this->data = $x; }
}
// TODO: fix the return type of the function test
function test(): Store<int> {
$data = 'Hello world!';
$x = new Store($data);
return $x;
}
<?hh
// You can create your own generics!
class Store<T> {
public function __construct(private T $data) {}
public function get(): T { return $this->data; }
public function set(T $x): void { $this->data = $x; }
}
// TODO: fix the return type of the function test
function test(): Store<string> {
$data = 'Hello world!';
$x = new Store($data);
return $x;
}
Exercise 15
<?hh
// You can specify constraints on generics.
interface MyInterface {
public function foo(): void;
}
// TODO: uncomment 'as MyInterface'
// T as MyInterface means any object as long as
// it implements MyInterface
function call_foo<T /* as MyInterface */>(T $x): T {
$x->foo();
return $x;
}
<?hh
// You can specify constraints on generics.
interface MyInterface {
public function foo(): void;
}
// TODO: uncomment 'as MyInterface'
// T as MyInterface means any object as long as
// it implements MyInterface
function call_foo<T as MyInterface>(T $x): T {
$x->foo();
return $x;
}
Exercise 16
<?hh
// The type 'this' always points to the most derived type
class MyBaseClass {
protected int $count = 0;
// TODO: replace 'MyBaseClass' by 'this'
public function add1(): MyBaseClass {
$this->count += 1;
return $this;
}
}
class MyDerivedClass extends MyBaseClass {
public function print_count(): void { echo $this->count; }
}
function test(): void {
$x = new MyDerivedClass();
$x->add1()->print_count();
}
<?hh
// The type 'this' always points to the most derived type
class MyBaseClass {
protected int $count = 0;
// TODO: replace 'MyBaseClass' by 'this'
public function add1(): this {
$this->count += 1;
return $this;
}
}
class MyDerivedClass extends MyBaseClass {
public function print_count(): void { echo $this->count; }
}
function test(): void {
$x = new MyDerivedClass();
$x->add1()->print_count();
}
Exercise 17
<?hh
// When a type is too long, you can use a type alias.
type Matrix<T> = Vector<Vector<T>>;
function first_row<T>(Matrix<T> $matrix): Vector<T> {
return $matrix[0];
}
<?hh
// When a type is too long, you can use a type alias.
type Matrix<T> = Vector<Vector<T>>;
function first_row<T>(Matrix<T> $matrix): Vector<T> {
return $matrix[0];
}
// タイプが長い時は別名を指定出来ます
Exercise 18
<?hh
// Tuples represent fixed size arrays.
// TODO: fix the return type.
function my_first_pair((int, bool) $pair): int {
list($_, $result) = $pair;
return $result;
}
<?hh
// Tuples represent fixed size arrays.
// TODO: fix the return type.
function my_first_pair((int, bool) $pair): int {
list($result, $_) = $pair;
return $result;
}
Exercise 19
<?hh
// Shapes can be used for arrays with constant string keys.
type my_shape = shape(
'field1' => int,
'field2' => bool,
);
function first_shape(): my_shape {
$result = shape('field1' => 1);
// TODO: set 'field2' to the value true
// on $result to complete the shape.
return $result;
}
<?hh
// Shapes can be used for arrays with constant string keys.
type my_shape = shape(
'field1' => int,
'field2' => bool,
);
function first_shape(): my_shape {
$result = my_shape(1, true);
// TODO: set 'field2' to the value true
// on $result to complete the shape.
return $result;
}
Exercise 20
<?hh
// You can specify the types of functions too.
function apply_int<T>((function(int): T) $callback, int $value): T {
// TODO: return $callback($value)
}
<?hh
// You can specify the types of functions too.
function apply_int<T>((function(int): T) $callback, int $value): T {
return $callback($value);
}
Exercise 21
<?hh
// XHP is useful to build html (or xml) elements.
// The escaping is done automatically, it is important to avoid
// security issues (XSS attacks).
function build_paragraph(string $text, string $style): :div {
return
<div style={$style}>
<p>{$text}</p>
</div>;
}
<?hh
// XHP is useful to build html (or xml) elements.
// The escaping is done automatically, it is important to avoid
// security issues (XSS attacks).
function build_paragraph(string $text, string $style): :div {
return
<div style={$style}>
<p>{$text}</p>
</div>;
}
// XHP記法でHTMLまたはXMLを構築することにより、クロスサイトスクリプティング対
策が実現できます
Exercise 22
<?hh
/* Opaque types let you hide the representation of a type.
*
* The definition below introduces the new type 'user_id'
* that will only be compatible with 'int' within this file.
* Outside of this file, 'user_id' becomes "opaque"; it won't
* be compatible with 'int' anymore.
*/
newtype user_id = int;
function make_user_id(int $x): user_id {
// Do some checks ...
return $x;
}
// You should only use this function for rendering
function user_id_to_int(user_id $x): int {
return $x;
}
<?hh
/* Opaque types let you hide the representation of a type.
*
* The definition below introduces the new type 'user_id'
* that will only be compatible with 'int' within this file.
* Outside of this file, 'user_id' becomes "opaque"; it won't
* be compatible with 'int' anymore.
*/
newtype user_id = int;
function make_user_id(int $x): user_id {
// Do some checks ...
return $x;
}
// You should only use this function for rendering
function user_id_to_int(user_id $x): int {
return $x;
}
// user_idタイプを作る事により、このファイルで型を管理し、他のファイルではuser_id型になります
Exercise 23
<?hh
class MyBaseClass {
// TODO: fix the typo in the name of the method.
public function get_uuser(): MyUser {
return new MyUser();
}
}
class MyDerivedClass extends MyBaseClass {
/* <<Override>> is used to specify that get_user has been inherited.
* When that's not the case, Hack gives an error.
*/
<<Override>> public function get_user(): MyUser {
return new MyUser();
}
}
<?hh
class MyBaseClass {
// TODO: fix the typo in the name of the method.
public function get_user(): MyUser {
return new MyUser();
}
}
class MyDerivedClass extends MyBaseClass {
/* <<Override>> is used to specify that get_user has been inherited.
* When that's not the case, Hack gives an error.
*/
<<Override>> public function get_user(): MyUser {
return new MyUser();
}
}
Exercise 24
<?hh
class C { protected function bar(): void {} }
interface I { public function foo(): void; }
// 'require' lets you specify what the trait needs to work properly.
trait T {
// The class using the trait must extend 'C'
require extends C;
// TODO: uncomment the next line to fix the error
// require implements I;
public function do_stuff(): void {
$this->bar(); // We can access bar because we used "require extends"
$this->foo();
}
}
<?hh
class C { protected function bar(): void {} }
interface I { public function foo(): void; }
// 'require' lets you specify what the trait needs to work properly.
trait T {
// The class using the trait must extend 'C'
require extends C;
// TODO: uncomment the next line to fix the error
require implements I;
public function do_stuff(): void {
$this->bar(); // We can access bar because we used "require extends"
$this->foo();
}
}
// Congratulations! You are done!
次回やりたいこと
1. WordPressを移行してみる
2. Hackで書き直す
3. ベンチマークを取って比較する
アップデート出来なくなるのでコピーで作業します (;´Д`)
ご清聴ありがとうございました

More Related Content

What's hot

AngularJS Compile Process
AngularJS Compile ProcessAngularJS Compile Process
AngularJS Compile ProcessEyal Vardi
 
React, Redux and es6/7
React, Redux and es6/7React, Redux and es6/7
React, Redux and es6/7Dongho Cho
 
AngulrJS Overview
AngulrJS OverviewAngulrJS Overview
AngulrJS OverviewEyal Vardi
 
AngularJS $http Interceptors (Explanation and Examples)
AngularJS $http Interceptors (Explanation and Examples)AngularJS $http Interceptors (Explanation and Examples)
AngularJS $http Interceptors (Explanation and Examples)Brian Swartzfager
 
AngularJS Routing
AngularJS RoutingAngularJS Routing
AngularJS RoutingEyal Vardi
 
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-Tsuyoshi Yamamoto
 
AngularJS Directives
AngularJS DirectivesAngularJS Directives
AngularJS DirectivesEyal Vardi
 
History of jQuery
History of jQueryHistory of jQuery
History of jQueryjeresig
 
Why You Shouldn't Write OO
Why You Shouldn't Write OO Why You Shouldn't Write OO
Why You Shouldn't Write OO Yehuda Katz
 
お題でGroovyプログラミング: Part A
お題でGroovyプログラミング: Part Aお題でGroovyプログラミング: Part A
お題でGroovyプログラミング: Part AKazuchika Sekiya
 
Client Side MVC & Angular
Client Side MVC & AngularClient Side MVC & Angular
Client Side MVC & AngularAlexe Bogdan
 
Improving the performance of Odoo deployments
Improving the performance of Odoo deploymentsImproving the performance of Odoo deployments
Improving the performance of Odoo deploymentsOdoo
 
Let's Build A Gutenberg Block | WordCamp Europe 2018
Let's Build A Gutenberg Block | WordCamp Europe 2018Let's Build A Gutenberg Block | WordCamp Europe 2018
Let's Build A Gutenberg Block | WordCamp Europe 2018Lara Schenck
 
Upgrade your javascript to drupal 8
Upgrade your javascript to drupal 8Upgrade your javascript to drupal 8
Upgrade your javascript to drupal 8Théodore Biadala
 
Drupal.js: Best Practices for Managing Javascript in Drupal
Drupal.js: Best Practices for Managing Javascript in DrupalDrupal.js: Best Practices for Managing Javascript in Drupal
Drupal.js: Best Practices for Managing Javascript in DrupalBryan Braun
 
Cервер на Go для мобильной стратегии
Cервер на Go для мобильной стратегииCервер на Go для мобильной стратегии
Cервер на Go для мобильной стратегииArtem Kovardin
 

What's hot (20)

Javascript - Beyond-jQuery
Javascript - Beyond-jQueryJavascript - Beyond-jQuery
Javascript - Beyond-jQuery
 
AngularJS Compile Process
AngularJS Compile ProcessAngularJS Compile Process
AngularJS Compile Process
 
Introducing jQuery
Introducing jQueryIntroducing jQuery
Introducing jQuery
 
React, Redux and es6/7
React, Redux and es6/7React, Redux and es6/7
React, Redux and es6/7
 
AngulrJS Overview
AngulrJS OverviewAngulrJS Overview
AngulrJS Overview
 
AngularJS $http Interceptors (Explanation and Examples)
AngularJS $http Interceptors (Explanation and Examples)AngularJS $http Interceptors (Explanation and Examples)
AngularJS $http Interceptors (Explanation and Examples)
 
AngularJS Routing
AngularJS RoutingAngularJS Routing
AngularJS Routing
 
JavaScript patterns
JavaScript patternsJavaScript patterns
JavaScript patterns
 
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
 
AngularJS Directives
AngularJS DirectivesAngularJS Directives
AngularJS Directives
 
History of jQuery
History of jQueryHistory of jQuery
History of jQuery
 
Why You Shouldn't Write OO
Why You Shouldn't Write OO Why You Shouldn't Write OO
Why You Shouldn't Write OO
 
Postman On Steroids
Postman On SteroidsPostman On Steroids
Postman On Steroids
 
お題でGroovyプログラミング: Part A
お題でGroovyプログラミング: Part Aお題でGroovyプログラミング: Part A
お題でGroovyプログラミング: Part A
 
Client Side MVC & Angular
Client Side MVC & AngularClient Side MVC & Angular
Client Side MVC & Angular
 
Improving the performance of Odoo deployments
Improving the performance of Odoo deploymentsImproving the performance of Odoo deployments
Improving the performance of Odoo deployments
 
Let's Build A Gutenberg Block | WordCamp Europe 2018
Let's Build A Gutenberg Block | WordCamp Europe 2018Let's Build A Gutenberg Block | WordCamp Europe 2018
Let's Build A Gutenberg Block | WordCamp Europe 2018
 
Upgrade your javascript to drupal 8
Upgrade your javascript to drupal 8Upgrade your javascript to drupal 8
Upgrade your javascript to drupal 8
 
Drupal.js: Best Practices for Managing Javascript in Drupal
Drupal.js: Best Practices for Managing Javascript in DrupalDrupal.js: Best Practices for Managing Javascript in Drupal
Drupal.js: Best Practices for Managing Javascript in Drupal
 
Cервер на Go для мобильной стратегии
Cервер на Go для мобильной стратегииCервер на Go для мобильной стратегии
Cервер на Go для мобильной стратегии
 

Similar to Hack tutorial

PHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolvePHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolveXSolve
 
Advanced php testing in action
Advanced php testing in actionAdvanced php testing in action
Advanced php testing in actionJace Ju
 
Go OO! - Real-life Design Patterns in PHP 5
Go OO! - Real-life Design Patterns in PHP 5Go OO! - Real-life Design Patterns in PHP 5
Go OO! - Real-life Design Patterns in PHP 5Stephan Schmidt
 
Object Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPObject Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPWildan Maulana
 
Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Nikita Popov
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript IntroductionDmitry Sheiko
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For BeginnersJonathan Wage
 
PHP 5.3 Overview
PHP 5.3 OverviewPHP 5.3 Overview
PHP 5.3 Overviewjsmith92
 
Javascript & Ajax Basics
Javascript & Ajax BasicsJavascript & Ajax Basics
Javascript & Ajax BasicsRichard Paul
 
PHP7 - Scalar Type Hints & Return Types
PHP7 - Scalar Type Hints & Return TypesPHP7 - Scalar Type Hints & Return Types
PHP7 - Scalar Type Hints & Return TypesEric Poe
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest UpdatesIftekhar Eather
 

Similar to Hack tutorial (20)

PHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolvePHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolve
 
Unittests für Dummies
Unittests für DummiesUnittests für Dummies
Unittests für Dummies
 
Mocking Demystified
Mocking DemystifiedMocking Demystified
Mocking Demystified
 
Advanced php testing in action
Advanced php testing in actionAdvanced php testing in action
Advanced php testing in action
 
Go OO! - Real-life Design Patterns in PHP 5
Go OO! - Real-life Design Patterns in PHP 5Go OO! - Real-life Design Patterns in PHP 5
Go OO! - Real-life Design Patterns in PHP 5
 
Object Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPObject Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOP
 
PHP pod mikroskopom
PHP pod mikroskopomPHP pod mikroskopom
PHP pod mikroskopom
 
Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?
 
php AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdfphp AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdf
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
PHP variables
PHP  variablesPHP  variables
PHP variables
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For Beginners
 
Smelling your code
Smelling your codeSmelling your code
Smelling your code
 
PHP 5.3 Overview
PHP 5.3 OverviewPHP 5.3 Overview
PHP 5.3 Overview
 
PHP PPT FILE
PHP PPT FILEPHP PPT FILE
PHP PPT FILE
 
Php 5.6
Php 5.6Php 5.6
Php 5.6
 
Javascript & Ajax Basics
Javascript & Ajax BasicsJavascript & Ajax Basics
Javascript & Ajax Basics
 
PHP7 - Scalar Type Hints & Return Types
PHP7 - Scalar Type Hints & Return TypesPHP7 - Scalar Type Hints & Return Types
PHP7 - Scalar Type Hints & Return Types
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest Updates
 

More from Wakana Yoshizawa

情熱の入社オンボーディ ング~コスト下げる~
情熱の入社オンボーディ ング~コスト下げる~情熱の入社オンボーディ ング~コスト下げる~
情熱の入社オンボーディ ング~コスト下げる~Wakana Yoshizawa
 
妊娠子育て中にReact js動画コースをudemyで公開するまで
妊娠子育て中にReact js動画コースをudemyで公開するまで妊娠子育て中にReact js動画コースをudemyで公開するまで
妊娠子育て中にReact js動画コースをudemyで公開するまでWakana Yoshizawa
 
Braintree+facebook messangerでチケット購入
Braintree+facebook messangerでチケット購入Braintree+facebook messangerでチケット購入
Braintree+facebook messangerでチケット購入Wakana Yoshizawa
 
Djangoを使って爆速でapi作成した
Djangoを使って爆速でapi作成したDjangoを使って爆速でapi作成した
Djangoを使って爆速でapi作成したWakana Yoshizawa
 
開発前にインタビューをして分かったこと 吉澤和香奈
開発前にインタビューをして分かったこと 吉澤和香奈開発前にインタビューをして分かったこと 吉澤和香奈
開発前にインタビューをして分かったこと 吉澤和香奈Wakana Yoshizawa
 
Webサーバー監視tips 吉澤和香奈
Webサーバー監視tips 吉澤和香奈Webサーバー監視tips 吉澤和香奈
Webサーバー監視tips 吉澤和香奈Wakana Yoshizawa
 
前期反省&下期目標 吉澤和香奈
前期反省&下期目標 吉澤和香奈前期反省&下期目標 吉澤和香奈
前期反省&下期目標 吉澤和香奈Wakana Yoshizawa
 
自己紹介&自社紹介 吉澤和香奈
自己紹介&自社紹介 吉澤和香奈自己紹介&自社紹介 吉澤和香奈
自己紹介&自社紹介 吉澤和香奈Wakana Yoshizawa
 

More from Wakana Yoshizawa (8)

情熱の入社オンボーディ ング~コスト下げる~
情熱の入社オンボーディ ング~コスト下げる~情熱の入社オンボーディ ング~コスト下げる~
情熱の入社オンボーディ ング~コスト下げる~
 
妊娠子育て中にReact js動画コースをudemyで公開するまで
妊娠子育て中にReact js動画コースをudemyで公開するまで妊娠子育て中にReact js動画コースをudemyで公開するまで
妊娠子育て中にReact js動画コースをudemyで公開するまで
 
Braintree+facebook messangerでチケット購入
Braintree+facebook messangerでチケット購入Braintree+facebook messangerでチケット購入
Braintree+facebook messangerでチケット購入
 
Djangoを使って爆速でapi作成した
Djangoを使って爆速でapi作成したDjangoを使って爆速でapi作成した
Djangoを使って爆速でapi作成した
 
開発前にインタビューをして分かったこと 吉澤和香奈
開発前にインタビューをして分かったこと 吉澤和香奈開発前にインタビューをして分かったこと 吉澤和香奈
開発前にインタビューをして分かったこと 吉澤和香奈
 
Webサーバー監視tips 吉澤和香奈
Webサーバー監視tips 吉澤和香奈Webサーバー監視tips 吉澤和香奈
Webサーバー監視tips 吉澤和香奈
 
前期反省&下期目標 吉澤和香奈
前期反省&下期目標 吉澤和香奈前期反省&下期目標 吉澤和香奈
前期反省&下期目標 吉澤和香奈
 
自己紹介&自社紹介 吉澤和香奈
自己紹介&自社紹介 吉澤和香奈自己紹介&自社紹介 吉澤和香奈
自己紹介&自社紹介 吉澤和香奈
 

Recently uploaded

home automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadhome automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadaditya806802
 
National Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfNational Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfRajuKanojiya4
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONjhunlian
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptMadan Karki
 
Industrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptIndustrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptNarmatha D
 
Steel Structures - Building technology.pptx
Steel Structures - Building technology.pptxSteel Structures - Building technology.pptx
Steel Structures - Building technology.pptxNikhil Raut
 
Vishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsVishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsSachinPawar510423
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating SystemRashmi Bhat
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
The SRE Report 2024 - Great Findings for the teams
The SRE Report 2024 - Great Findings for the teamsThe SRE Report 2024 - Great Findings for the teams
The SRE Report 2024 - Great Findings for the teamsDILIPKUMARMONDAL6
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionMebane Rash
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Solving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptSolving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptJasonTagapanGulla
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating SystemRashmi Bhat
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgsaravananr517913
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating SystemRashmi Bhat
 
Mine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxMine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxRomil Mishra
 

Recently uploaded (20)

home automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadhome automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasad
 
National Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfNational Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdf
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.ppt
 
Industrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptIndustrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.ppt
 
Steel Structures - Building technology.pptx
Steel Structures - Building technology.pptxSteel Structures - Building technology.pptx
Steel Structures - Building technology.pptx
 
Vishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsVishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documents
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating System
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
The SRE Report 2024 - Great Findings for the teams
The SRE Report 2024 - Great Findings for the teamsThe SRE Report 2024 - Great Findings for the teams
The SRE Report 2024 - Great Findings for the teams
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of Action
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Solving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptSolving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.ppt
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating System
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating System
 
Mine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxMine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptx
 

Hack tutorial

  • 2. 自己紹介 ↳ 吉澤和香奈 ↳ カツラじゃありません ↳ 1987/6/30生まれO型 ↳ PHPに出会ったのは2012 年の夏です ↳ HNは「wakana」「わかな だょ〜」「ブ〜バ〜」等、気 分により多数あります
  • 14. /***************************************************/ /****************** Hack Tutorial ******************/ /***************************************************/ /************ Click 'Next' to get started! *********/
  • 16. <?php // ^-- FIXME: replace <?php with <?hh // A Hack file always starts with <?hh
  • 17. <?hh // ^-- FIXME: replace <?php with <?hh // A Hack file always starts with <?hh
  • 19. <?hh // Hack functions are annotated with types. function my_negation(bool $x): bool { return !$x; } // FIXME: annotate this function parameter // and return with the type 'int'. function add_one(/* TODO */ $x): /* TODO */ { return $x+1; }
  • 20. <?hh // Hack functions are annotated with types. function my_negation(bool $x): bool { return !$x; } // FIXME: annotate this function parameter // and return with the type 'int'. function add_one(int $x): int { return $x+1; }
  • 22. <?hh /* Hack errors come in multiple parts. * Hover over the underlined parts! */ function add_one(int $x): int { return $x+1; } function test(): void { $my_string = 'hello'; // Some clever code ... add_one($my_string); }
  • 23. <?hh /* Hack errors come in multiple parts. * Hover over the underlined parts! */ function add_one(int $x): int { return $x+1; } function test(): void { $my_string = 'hello'; // Some clever code ... add_one((int) $my_string); }
  • 25. <?hh // Prefixing a type with '?' permits null. // TODO: fix the type of the parameter $x to permit null. function f(int $x): void { var_dump($x); } function test(): void { f(123); f(null); }
  • 26. <?hh // Prefixing a type with '?' permits null. // TODO: fix the type of the parameter $x to permit null. function f(?int $x): void { var_dump($x); } function test(): void { f(123); f(null); }
  • 28. <?hh interface User { public function getName(): string; } function get_user_name(?User $user): string { if($user !== null) { // We checked that $user was not null. // Its type is now 'User'. /* TODO: return $user->getName() */ } return '<invalid name>'; } function test(User $user) { $name1 = get_user_name($user); $name2 = get_user_name(null); }
  • 29. <?hh interface User { public function getName(): string; } function get_user_name(?User $user): string { if($user !== null) { // We checked that $user was not null. // Its type is now 'User'. return $user->getName(); } return '<invalid name>'; } function test(User $user) { $name1 = get_user_name($user); $name2 = get_user_name(null); }
  • 31. <?hh interface User { public function getName(): string; } // There are many ways to handle null values. // Throwing an exception is one of them. function get_user_name(?User $user): string { if($user === null) { throw new RuntimeException('Invalid user name'); } /* TODO: return $user->getName() */ } function test(User $user) { $name1 = get_user_name($user); $name2 = get_user_name(null); }
  • 32. <?hh interface User { public function getName(): string; } // There are many ways to handle null values. // Throwing an exception is one of them. function get_user_name(?User $user): string { if($user === null) { throw new RuntimeException('Invalid user name'); } return $user->getName(); } function test(User $user) { $name1 = get_user_name($user); $name2 = get_user_name(null); }
  • 34. <?hh // Hack introduces new collection types (Vector, Set and Map). function test(): int { // Vector is preferred over array(1, 2, 3) $vector = Vector {1, 2, 3}; $sum = 0; foreach ($vector as $val) { $sum += $val; } return $sum; }
  • 35. <?hh // Hack introduces new collection types (Vector, Set and Map). function test(): int { // Vector is preferred over array(1, 2, 3) $vector = Vector {1, 2, 3}; $sum = 0; foreach ($vector as $val) { $sum += $val; } return $sum; } // Arrayよりも高速なVectorが使えます
  • 37. <?hh // Hack uses generics for Collection types. // TODO: fix the return type of the function 'test' function test(): Vector<string> { $vector = Vector {1, 2, 3}; return $vector; }
  • 38. <?hh // Hack uses generics for Collection types. // TODO: fix the return type of the function 'test' function test(): Vector<string> { $vector = Vector {“1”, “2”, “3”}; return $vector; }
  • 40. <?hh function vector_add1(Vector<int> $v): Vector<int> { // Example of lambda expressions. return $v->map($x ==> $x + 1); } function vector_mult2(Vector<int> $v): Vector<int> { // TODO: write a function multiplying all the elements by 2 }
  • 41. <?hh function vector_add1(Vector<int> $v): Vector<int> { // Example of lambda expressions. return $v->map($x ==> $x + 1); } function vector_mult2(Vector<int> $v): Vector<int> { // TODO: write a function multiplying all the elements by 2 return $v->map($x ==> $x * 2); }
  • 43. /* * Congratulations! * You completed the beginner's tutorial. * * Click next to continue in expert mode. */
  • 45. <?hh // All the members of a class must be initialized class Point { private float $x; private float $y; public function __construct(float $x, float $y) { $this->x = $x; // FIXME: initalize the member 'y' } }
  • 46. <?hh // All the members of a class must be initialized class Point { private float $x; private float $y; public function __construct(float $x, float $y) { $this->x = $x; // FIXME: initalize the member 'y' $this->y = $y; } }
  • 48. <?hh // Check out this new syntax! // It's shorter and does the same thing ... class Point { public function __construct( private float $x, private float $y ) {} }
  • 49. <?hh // Check out this new syntax! // It's shorter and does the same thing ... class Point { public function __construct( private float $x, private float $y ) {} } // このような新しい書き方で今までより短くなります
  • 51. <?hh // You can create your own generics! class Store<T> { public function __construct(private T $data) {} public function get(): T { return $this->data; } public function set(T $x): void { $this->data = $x; } } // TODO: fix the return type of the function test function test(): Store<int> { $data = 'Hello world!'; $x = new Store($data); return $x; }
  • 52. <?hh // You can create your own generics! class Store<T> { public function __construct(private T $data) {} public function get(): T { return $this->data; } public function set(T $x): void { $this->data = $x; } } // TODO: fix the return type of the function test function test(): Store<string> { $data = 'Hello world!'; $x = new Store($data); return $x; }
  • 54. <?hh // You can specify constraints on generics. interface MyInterface { public function foo(): void; } // TODO: uncomment 'as MyInterface' // T as MyInterface means any object as long as // it implements MyInterface function call_foo<T /* as MyInterface */>(T $x): T { $x->foo(); return $x; }
  • 55. <?hh // You can specify constraints on generics. interface MyInterface { public function foo(): void; } // TODO: uncomment 'as MyInterface' // T as MyInterface means any object as long as // it implements MyInterface function call_foo<T as MyInterface>(T $x): T { $x->foo(); return $x; }
  • 57. <?hh // The type 'this' always points to the most derived type class MyBaseClass { protected int $count = 0; // TODO: replace 'MyBaseClass' by 'this' public function add1(): MyBaseClass { $this->count += 1; return $this; } } class MyDerivedClass extends MyBaseClass { public function print_count(): void { echo $this->count; } } function test(): void { $x = new MyDerivedClass(); $x->add1()->print_count(); }
  • 58. <?hh // The type 'this' always points to the most derived type class MyBaseClass { protected int $count = 0; // TODO: replace 'MyBaseClass' by 'this' public function add1(): this { $this->count += 1; return $this; } } class MyDerivedClass extends MyBaseClass { public function print_count(): void { echo $this->count; } } function test(): void { $x = new MyDerivedClass(); $x->add1()->print_count(); }
  • 60. <?hh // When a type is too long, you can use a type alias. type Matrix<T> = Vector<Vector<T>>; function first_row<T>(Matrix<T> $matrix): Vector<T> { return $matrix[0]; }
  • 61. <?hh // When a type is too long, you can use a type alias. type Matrix<T> = Vector<Vector<T>>; function first_row<T>(Matrix<T> $matrix): Vector<T> { return $matrix[0]; } // タイプが長い時は別名を指定出来ます
  • 63. <?hh // Tuples represent fixed size arrays. // TODO: fix the return type. function my_first_pair((int, bool) $pair): int { list($_, $result) = $pair; return $result; }
  • 64. <?hh // Tuples represent fixed size arrays. // TODO: fix the return type. function my_first_pair((int, bool) $pair): int { list($result, $_) = $pair; return $result; }
  • 66. <?hh // Shapes can be used for arrays with constant string keys. type my_shape = shape( 'field1' => int, 'field2' => bool, ); function first_shape(): my_shape { $result = shape('field1' => 1); // TODO: set 'field2' to the value true // on $result to complete the shape. return $result; }
  • 67. <?hh // Shapes can be used for arrays with constant string keys. type my_shape = shape( 'field1' => int, 'field2' => bool, ); function first_shape(): my_shape { $result = my_shape(1, true); // TODO: set 'field2' to the value true // on $result to complete the shape. return $result; }
  • 69. <?hh // You can specify the types of functions too. function apply_int<T>((function(int): T) $callback, int $value): T { // TODO: return $callback($value) }
  • 70. <?hh // You can specify the types of functions too. function apply_int<T>((function(int): T) $callback, int $value): T { return $callback($value); }
  • 72. <?hh // XHP is useful to build html (or xml) elements. // The escaping is done automatically, it is important to avoid // security issues (XSS attacks). function build_paragraph(string $text, string $style): :div { return <div style={$style}> <p>{$text}</p> </div>; }
  • 73. <?hh // XHP is useful to build html (or xml) elements. // The escaping is done automatically, it is important to avoid // security issues (XSS attacks). function build_paragraph(string $text, string $style): :div { return <div style={$style}> <p>{$text}</p> </div>; } // XHP記法でHTMLまたはXMLを構築することにより、クロスサイトスクリプティング対 策が実現できます
  • 75. <?hh /* Opaque types let you hide the representation of a type. * * The definition below introduces the new type 'user_id' * that will only be compatible with 'int' within this file. * Outside of this file, 'user_id' becomes "opaque"; it won't * be compatible with 'int' anymore. */ newtype user_id = int; function make_user_id(int $x): user_id { // Do some checks ... return $x; } // You should only use this function for rendering function user_id_to_int(user_id $x): int { return $x; }
  • 76. <?hh /* Opaque types let you hide the representation of a type. * * The definition below introduces the new type 'user_id' * that will only be compatible with 'int' within this file. * Outside of this file, 'user_id' becomes "opaque"; it won't * be compatible with 'int' anymore. */ newtype user_id = int; function make_user_id(int $x): user_id { // Do some checks ... return $x; } // You should only use this function for rendering function user_id_to_int(user_id $x): int { return $x; } // user_idタイプを作る事により、このファイルで型を管理し、他のファイルではuser_id型になります
  • 78. <?hh class MyBaseClass { // TODO: fix the typo in the name of the method. public function get_uuser(): MyUser { return new MyUser(); } } class MyDerivedClass extends MyBaseClass { /* <<Override>> is used to specify that get_user has been inherited. * When that's not the case, Hack gives an error. */ <<Override>> public function get_user(): MyUser { return new MyUser(); } }
  • 79. <?hh class MyBaseClass { // TODO: fix the typo in the name of the method. public function get_user(): MyUser { return new MyUser(); } } class MyDerivedClass extends MyBaseClass { /* <<Override>> is used to specify that get_user has been inherited. * When that's not the case, Hack gives an error. */ <<Override>> public function get_user(): MyUser { return new MyUser(); } }
  • 81. <?hh class C { protected function bar(): void {} } interface I { public function foo(): void; } // 'require' lets you specify what the trait needs to work properly. trait T { // The class using the trait must extend 'C' require extends C; // TODO: uncomment the next line to fix the error // require implements I; public function do_stuff(): void { $this->bar(); // We can access bar because we used "require extends" $this->foo(); } }
  • 82. <?hh class C { protected function bar(): void {} } interface I { public function foo(): void; } // 'require' lets you specify what the trait needs to work properly. trait T { // The class using the trait must extend 'C' require extends C; // TODO: uncomment the next line to fix the error require implements I; public function do_stuff(): void { $this->bar(); // We can access bar because we used "require extends" $this->foo(); } }
  • 84. 次回やりたいこと 1. WordPressを移行してみる 2. Hackで書き直す 3. ベンチマークを取って比較する アップデート出来なくなるのでコピーで作業します (;´Д`)