Alejandro Marcu gave a presentation on programming in Hack. He began with his background in programming languages over the years. He then discussed Hack, noting that it started as a strict mode for PHP in 2012 and is now the only language that can be executed with HHVM. He covered key Hack features like types, collections, async, XHP for generating HTML, and code generation capabilities. He concluded with information on learning resources for Hack.
2. 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. 3
1. Introduction
2. Hack Types
3. Collections
4. Async
5. XHP
6. Hack Codegen
What You Will Learn Today
5. 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. 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. 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. 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
10. 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. 11
References
Global statements
Top level code
Variable variables
Dynamic properties
Pseudo-functions
isset
empty
Unset
goto
Etc..
Unsupported features in Hack
12. 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
44. 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 HHAsiojoin
Make parallel calls using helpers in HHAsio
(such as v,vm,m,mf, etc.)
Async
46. 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
53. 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. 54
XHP tags are replaced with constructors. E.g.
will be replaced with:
XHP internals
56. 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. 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