Programming in hack

66
Programming in Hack Alejandro Marcu Dutch PHP Conference 2016

Transcript of Programming in hack

Page 1: Programming in hack

Programming in Hack

Alejandro Marcu

Dutch PHP Conference 2016

Page 2: Programming in hack

2

Started programming Logo at 8 years old

Then moved to Basic, Turbo Pascal, C++, Java

2001 – 2004 Various programming jobs in Argentina

2004 – 2008: TopCoder 2009 – 2015: Facebook

Alejandro Marcu

Page 3: Programming in hack

3

1. Introduction

2. Hack Types

3. Collections

4. Async

5. XHP

6. Hack Codegen

What You Will Learn Today

Page 4: Programming in hack

Introduction

Page 5: Programming in hack

5

HHVM history

2007 - Hackathon project: convert PHP to c++

2009 – HipHop (HPHPc) in prod in Facebook

2010 – Open sourced HipHop for PHP

2010 – Started the HHVM project

2013 – HHVM in prod in Facebook

2013 – HHVM open sourced

Page 6: Programming in hack

6

In 2014 Wikipedia migrated to HHVM:

CPU load went from 50% to 10%

Mean page save time went 6s to 3s

Average page load time went from 1.3s to 0.9s

PHP 7 performance is close to HHVM

Example of migration to HHVM

Page 7: Programming in hack

7

Started in 2012 as “strict-mode” project

Grew to be much more than that

Open sourced in 2014

As of now, HHVM is the only execution engine that

runs Hack

Totally interoperable with PHP, allowing for a gradual

migration

Hack

Page 8: Programming in hack

8

Analyzes Hack programs statically

Monitors files changes in the background

Extremely fast, even in huge code bases

Can even be used by IDEs to autocomplete code

Typechecker

Page 9: Programming in hack

Typechecker

hacklang.org/tutorial/

Page 10: Programming in hack

10

Type annotations Collections Async XHP Attributes Constructor parameter

Promotion Enums Generics

Hack only features

Operators Lambda Null safe XHP attribute access Pipe operator

Shapes Trait and interfaces

requirements Type aliases

Page 11: Programming in hack

11

References Global statements Top level code Variable variables Dynamic properties Pseudo-functions

isset empty Unset

goto Etc..

Unsupported features in Hack

Page 12: Programming in hack

12

<?hh //strict Type annotations required Can’t call PHP No top level code No references

<?hh // partial or<?hh

The following is allowed but not checked:• Calls to PHP• Top level code• References

Type annotations not required but checked if present

<?hh // decl Not checked at all Functions and classes are indexed Use it only as the first step to migrate to

Hack

Typechecker modes

Page 13: Programming in hack

Type annotations

Page 14: Programming in hack

14

Types

Primitive types bool int float string array resource

Combined primitive types num (int or float) arraykey (int or string)

Object types Name of classes or interfaces

Page 15: Programming in hack

Return types

void can be used only as a return type

Page 16: Programming in hack

Parameters

Page 17: Programming in hack

Nullable parameters

Page 18: Programming in hack

Local variables

Page 19: Programming in hack

Class attributes

Page 20: Programming in hack

Class attributes

Page 21: Programming in hack

Class attributes

Page 22: Programming in hack

Constructor argument promotion

Page 23: Programming in hack

Enums

Page 24: Programming in hack

Enums

Page 25: Programming in hack

Enums

Page 26: Programming in hack

Generics

Page 27: Programming in hack

Arrays with generics

Page 28: Programming in hack

Collections

Page 29: Programming in hack

Vector0-indexed list

MapKeys: int or str.Values: anything

SetUnique elements

Collections overview

Page 30: Programming in hack

Vector

Page 31: Programming in hack

Map

Page 32: Programming in hack

Set

Page 33: Programming in hack

Immutability

Page 34: Programming in hack

Immutability

Page 35: Programming in hack

Immutability

Page 36: Programming in hack

Async

Page 37: Programming in hack

37

Single thread blocking

Single thread non-blocking

Multithread

Threads

Page 38: Programming in hack

Single Thread blocking example

Page 39: Programming in hack

Single Thread blocking example

Page 40: Programming in hack

Single Thread blocking example

Page 41: Programming in hack

Async example

Page 42: Programming in hack

Async example

Page 43: Programming in hack

Async example

Page 44: Programming in hack

44

Use the keyword async before function The return type must be Awaitable<…> Call async from async using await Call async from non-async using HH\Asio\join Make parallel calls using helpers in HH\Asio\

(such as v,vm,m,mf, etc.)

Async

Page 45: Programming in hack

XHP

Page 46: Programming in hack

46

What is XHP?

XHP is used to generate HTML in PHP or Hack No templating No string concatenating

Represent HTML as a tree of objects Familiar XML syntax Secure Strong validations Extensible

Page 47: Programming in hack

Hello World

composer.json

xhp.php

Page 48: Programming in hack

Hello World

Page 49: Programming in hack

Adding children

Page 50: Programming in hack

Custom class definition

Page 51: Programming in hack

Inheriting and transferring attributes

Page 52: Programming in hack

Async components

Page 53: Programming in hack

53

XHP parser transforms it into regular Hack code:

XHP class names are replaced:

First char colon is replaced with xhp_

Other colons are replaced with two underscores

Hyphens are replaced with an underscore

E.g. :ui:nav:header is replaced with xhp_ui__nav__header

XHP internals

Page 54: Programming in hack

54

XHP tags are replaced with constructors. E.g.

will be replaced with:

XHP internals

Page 55: Programming in hack

Hack Codegen

Page 56: Programming in hack

56

Why code generation?

Code generation: writing code that writes code

Higher level of abstraction

Generate boilerplate code

Generate code from almost static data in database

Replace usage of __call

Page 57: Programming in hack

57

Easy to create files, functions, classes, methods, arrays, collections, etc

Support for partially generated files Files can be signed No string concatenation No need to indent or add spaces https://github.com/facebook/hack-codegen/ Composer: require facebook/hack-codegen

Hack Codegen

Page 58: Programming in hack

Hack Codegen example

Page 59: Programming in hack

Hack Codegen example output

Page 60: Programming in hack

ORM code generation

Page 61: Programming in hack

ORM code generation

Page 62: Programming in hack

ORM code generation

Page 63: Programming in hack

ORM code generation

Page 64: Programming in hack

Learning Resources

Page 65: Programming in hack

65

Learning resources

http://hacklang.org/ : installation, tutorial, docs

“Hack & HHVM” book

Page 66: Programming in hack

Contact Information

[email protected]

/alejandro.marcu

/alejandromarcu

@AlejandroMarcu

/in/alejandromarcu