Programming in hack
-
Upload
alejandro-marcu -
Category
Software
-
view
108 -
download
0
Transcript of Programming in hack
Programming in Hack
Alejandro Marcu
Dutch PHP Conference 2016
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
3
1. Introduction
2. Hack Types
3. Collections
4. Async
5. XHP
6. Hack Codegen
What You Will Learn Today
Introduction
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
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
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
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
Typechecker
hacklang.org/tutorial/
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
11
References Global statements Top level code Variable variables Dynamic properties Pseudo-functions
isset empty Unset
goto Etc..
Unsupported features 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
Type annotations
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
Return types
void can be used only as a return type
Parameters
Nullable parameters
Local variables
Class attributes
Class attributes
Class attributes
Constructor argument promotion
Enums
Enums
Enums
Generics
Arrays with generics
Collections
Vector0-indexed list
MapKeys: int or str.Values: anything
SetUnique elements
Collections overview
Vector
Map
Set
Immutability
Immutability
Immutability
Async
37
Single thread blocking
Single thread non-blocking
Multithread
Threads
Single Thread blocking example
Single Thread blocking example
Single Thread blocking example
Async example
Async example
Async example
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
XHP
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
Hello World
composer.json
xhp.php
Hello World
Adding children
Custom class definition
Inheriting and transferring attributes
Async components
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
54
XHP tags are replaced with constructors. E.g.
will be replaced with:
XHP internals
Hack Codegen
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
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
Hack Codegen example
Hack Codegen example output
ORM code generation
ORM code generation
ORM code generation
ORM code generation
Learning Resources
65
Learning resources
http://hacklang.org/ : installation, tutorial, docs
“Hack & HHVM” book
Contact Information
/alejandro.marcu
/alejandromarcu
@AlejandroMarcu
/in/alejandromarcu