SlideShare a Scribd company logo
Hack programming language
Agenda
 What is the Hack Language ?
 Hack Modes
 Type Annotations
 Nullable
 Generics
 Collections
 Type Aliasing
 Constructor Argument Promotion
 PHP to Hack Conversion DEMO
Not on Agenda, but interesting
 Async - asynchronous programming support via
async/await
 XHP – an extension of the PHP and Hack syntax
where XML elements/blocks become valid
expressions
 Lamba expressions – a more advanced version of PHP
closures
What is the Hack Language ?
 Language for HHVM that interoperates with PHP
 Developed and used by Facebook
 Open-Sourced in March 2014
 Basically, PHP with a ton of extra features
 Hack = Fast development cycle of PHP + discipline of
strong typing
 Combines strong typing with weak typing => gradual
typing (you can strong type only the parts you want)
 Write cleaner/safer code while maintaining good
compatibility with existing PHP codebases
What is the Hack Language ? (II)
 Hack code uses “<?hh” instead of “<?php”. Not
mandatory but, if you do it, you won’t be able to jump
between Hack and HTML anymore
 Hack and PHP can coexist in the same project. Good if
you want an incremental switch to Hack.
 PHP code can call Hack code and vice-versa
 Closing tags (“?>”) are not supported
 Naming your files “*.hh” can be a good practice
Hack Modes (I)
 Intended for maximum flexibility converting PHP to
Hack
 Helps when running “hackificator”
 Each file can have only one Hack mode
 Default is “partial”
 Declared at the top: “<?hh // strict”
Hack Modes (II)
 Strict:
 <?hh // strict
 Type checker will catch every error
 All code in this mode must be correctly annotated
 Code in strict mode cannot call non-Hack code.
Hack Modes (III)
 Partial:
 <?hh // partial
 <?hh
 Default mode
 Allows calling non-Hack code
 Allows ommitting some function parameters
Hack Modes (IV)
 Decl:
 <?hh // decl
 used when annotating old APIs
 Allows Hack code written in strict mode to call into legacy
code. Just like in partial mode, but without having to fix
reported problems
Type Annotations (I)
 Readability by helping other developers understand the
purpose and intention of the code. Many used
comments to do this, but Hack formalizes it instead
 Correctness by forbidding unsafe coding practices
 Make use of automatic and solid refactoring tools of a
strong-typed language.
Type Annotations (II)
 class MyExampleClass {
 public int $number;
 public CustomClass $myObject;
 private string $theName;
 protected array $mystuff;

 function doStuff(string $name, bool $withHate): string {
 if ($withHate && $name === 'Satan') {
 return '666 HAHA';
 }
 return '';
 }
 }
Type Annotations (III)
 Possible annotations:
 Primitive types: int, string, float, bool, array
 User-defined classes: MyClass, Vector<mytype>
 Generics: MyClass<T>
 Mixed: mixed
 Void: void
 Typed arrays: array<Foo>, array<string, array<string, MyClass>>
 Tuples: e.g. tuple(string, bool)
 XHP elements
 Closures: (function(type_1, type_2): return_type)
 Resources: resource
 this: this
Nullable
 Safer way to deal with nulls
 In type annotation, add “?” to the type to make it
nullable. e.g. ?string
 Normal PHP code does allow some annotation:
 public function doStuff(MyClass $obj)
 But passing null to this results in a fatal error
 So in Hack you can do this:
 public function doStuff(?MyClass $obj)
 It’s best not to abuse this feature, but to use it when you
really need it
Generics (I)
 Allows classes and methods to be parameterized
 Generics code can be statically checked for correctness
 Offers a great alternative against using a top-level object + a
bunch of instanceof calls + type casts
 In most languages, classes must be used as types. But Hack
allows primitives too
 They are immutable: once a type has been associated, it can
not be changed
 In Hack, objects can be used as types too
 Nullable types are also supported
 Interfaces and Traits support Generics too
 See docs for more details
Generics (II)
 class MyClass<K> {
 private K $param;
 public function __construct(K $param) {
 $this->param = $param;
 }
 public function getParameter(): K {
 return $this->param;
 }
 }
Collections (I)
 Classes specialized for data storage and retrieval
 PHP arrays are too general, they offer a “one-size-fits-all”
approach
 In both Hack and PHP, arrays are not objects. But
Collections are.
 Seeing the keyword "array" in a piece of code doesn't
make it clear how that array will be used
 But each Collection class is best suited for specific
situations.
 So code is clearer and, if used correctly, can be a
performance improvement
Collections (II)
 They are traversable with “foreach” loops
 Support bracket notations: $mycollection[$index]
 Since they are objects, they are not copied when passed
around as parameters
 They integrate and work very well with Generics
 Immutable variants of the Collection classes also exist.
This is to ensure that they are not changed when passed
around
 Classes are: Vector, Map, Set, Pair
 Immutable collections: ImmVector, ImmMap, ImmSet
Collections (III)
 Vector example:
 $vector = Vector {6, 5, 4, 9};
 $vector->add(97);
 $vector->add(31);
 $vector->get(1);
 $x = $vector[2];
 $vector->removeKey(2);
 $vector[] = 999;
 foreach ($vector as $key => $value) {...}
Collections (IV)
 Map example:
 $map = Map {“key1" => 1, “key2" => 2, “key3" => 3};
 $b = $map->get(“key2");
 $map->remove(“key2");
 $map->contains(“key1");
 foreach ($map as $key => $value) {...}
Type Aliasing
 Just like PHP has aliasing support in namespaces, so
does Hack provide the same functionality for any type
 Two modes of declaration:
 type MyType = string;
 newtype MyType = string;
 The 2nd is called “opaque aliasing” and is only visible in
the same file
 Composite types can also be declared:
 newtype Coordinate = (float, float);
 These are implemented as tuples, so that is how they must
be created in order to be used
Constructor Argument Promotion (I)
 Before:
 class User {
 private string $name;
 private string $email;
 private int $age;
 public function __construct(string $name, string $email, int $age)
{
 $this->name = $name;
 $this->email = $email;
 $this->age = $age;
 }
 }
Constructor Argument Promotion (II)
 After:
 class User {
 public function __construct(
 private string $name,
 private string $email,
 private int $age
 ) {}
 }
PHP to Hack
Conversion
DEMO
Q & A
Hack programming language

More Related Content

What's hot (20)

PDF
Back to the Future with TypeScript
Aleš Najmann
 
PPT
Introduction To C#
SAMIR BHOGAYTA
 
PPT
Lecture 9- Control Structures 1
Md. Imran Hossain Showrov
 
PDF
C++ idioms by example (Nov 2008)
Olve Maudal
 
PPTX
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
anshkhurana01
 
PDF
TDD in C - Recently Used List Kata
Olve Maudal
 
PDF
PHP 8: Process & Fixing Insanity
GeorgePeterBanyard
 
PDF
scala.reflect, Eugene Burmako
Vasil Remeniuk
 
PDF
JavaScript 101 - Class 1
Robert Pearce
 
PDF
Methods of debugging - Atomate.net
Vitalie Chiperi
 
PDF
Static types on javascript?! Type checking approaches to ensure healthy appli...
Arthur Puthin
 
DOC
20 C programs
navjoth
 
PDF
DDD on example of Symfony (Webcamp Odessa 2014)
Oleg Zinchenko
 
PDF
Introduction to Dart
RameshNair6
 
PDF
DTS s03e04 Typing
Tuenti
 
PPTX
Chap1introppt2php(finally done)
monikadeshmane
 
PPT
Lecture 11 - Functions
Md. Imran Hossain Showrov
 
Back to the Future with TypeScript
Aleš Najmann
 
Introduction To C#
SAMIR BHOGAYTA
 
Lecture 9- Control Structures 1
Md. Imran Hossain Showrov
 
C++ idioms by example (Nov 2008)
Olve Maudal
 
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
anshkhurana01
 
TDD in C - Recently Used List Kata
Olve Maudal
 
PHP 8: Process & Fixing Insanity
GeorgePeterBanyard
 
scala.reflect, Eugene Burmako
Vasil Remeniuk
 
JavaScript 101 - Class 1
Robert Pearce
 
Methods of debugging - Atomate.net
Vitalie Chiperi
 
Static types on javascript?! Type checking approaches to ensure healthy appli...
Arthur Puthin
 
20 C programs
navjoth
 
DDD on example of Symfony (Webcamp Odessa 2014)
Oleg Zinchenko
 
Introduction to Dart
RameshNair6
 
DTS s03e04 Typing
Tuenti
 
Chap1introppt2php(finally done)
monikadeshmane
 
Lecture 11 - Functions
Md. Imran Hossain Showrov
 

Viewers also liked (14)

PPTX
The World of PHP PSR Standards
Radu Murzea
 
PPTX
SymfonyCon 2015 - A symphony of developers
Radu Murzea
 
PPTX
PHP 7 - A look at the future
Radu Murzea
 
PPTX
God’s eye - Technology
Akshit Mareedu
 
PDF
On taxonomies of code and how to hack it
parallellns
 
PPT
Introduction to ethical hacking
ankit sarode
 
PPT
the best hacking ppt
fuckubitches
 
PDF
30 Brilliant marketing growth hack cards.
ClavainSkade
 
PPTX
Computer Hacking - An Introduction
Jayaseelan Vejayon
 
PPTX
Securing the Cloud
GGV Capital
 
PPTX
Hacking ppt
giridhar_sadasivuni
 
PDF
10 Event Technology Trends to Watch in 2016
Eventbrite UK
 
PDF
Publishing Production: From the Desktop to the Cloud
Deanta
 
PPTX
31 Best Growth Hacking Resources
Stephen Jeske
 
The World of PHP PSR Standards
Radu Murzea
 
SymfonyCon 2015 - A symphony of developers
Radu Murzea
 
PHP 7 - A look at the future
Radu Murzea
 
God’s eye - Technology
Akshit Mareedu
 
On taxonomies of code and how to hack it
parallellns
 
Introduction to ethical hacking
ankit sarode
 
the best hacking ppt
fuckubitches
 
30 Brilliant marketing growth hack cards.
ClavainSkade
 
Computer Hacking - An Introduction
Jayaseelan Vejayon
 
Securing the Cloud
GGV Capital
 
Hacking ppt
giridhar_sadasivuni
 
10 Event Technology Trends to Watch in 2016
Eventbrite UK
 
Publishing Production: From the Desktop to the Cloud
Deanta
 
31 Best Growth Hacking Resources
Stephen Jeske
 

Similar to Hack programming language (20)

PPTX
Welcome to hack
Timothy Chandler
 
PPTX
Programming in hack
Alejandro Marcu
 
PDF
HHVM and Hack: A quick introduction
Kuan Yen Heng
 
PDF
Hack the Future
Jason McCreary
 
RTF
Hack language
Ravindra Bhadane
 
PDF
PHP to Hack at Slack
Scott Sandler
 
PPTX
Why choose Hack/HHVM over PHP7
Yuji Otani
 
PPTX
Oops in php
Gourishankar R Pujar
 
PDF
Modern php
Charles Anderson
 
PDF
PHP 5.3 Overview
jsmith92
 
PPTX
Introduction to PHP and MySql basics.pptx
PriyankaKupneshi
 
PDF
08 Advanced PHP #burningkeyboards
Denis Ristic
 
KEY
Can't Miss Features of PHP 5.3 and 5.4
Jeff Carouth
 
PDF
0php 5-online-cheat-sheet-v1-3
Fafah Ranaivo
 
ODP
What's new, what's hot in PHP 5.3
Jeremy Coates
 
PDF
What is new in PHP
Haim Michael
 
PPTX
Introducing PHP Latest Updates
Iftekhar Eather
 
PPTX
20 cool features that is in PHP 7, we missed in PHP 5. Let walkthrough with t...
DrupalMumbai
 
PPTX
Php 5.4: New Language Features You Will Find Useful
David Engel
 
PDF
Practical PHP 5.3
Nate Abele
 
Welcome to hack
Timothy Chandler
 
Programming in hack
Alejandro Marcu
 
HHVM and Hack: A quick introduction
Kuan Yen Heng
 
Hack the Future
Jason McCreary
 
Hack language
Ravindra Bhadane
 
PHP to Hack at Slack
Scott Sandler
 
Why choose Hack/HHVM over PHP7
Yuji Otani
 
Modern php
Charles Anderson
 
PHP 5.3 Overview
jsmith92
 
Introduction to PHP and MySql basics.pptx
PriyankaKupneshi
 
08 Advanced PHP #burningkeyboards
Denis Ristic
 
Can't Miss Features of PHP 5.3 and 5.4
Jeff Carouth
 
0php 5-online-cheat-sheet-v1-3
Fafah Ranaivo
 
What's new, what's hot in PHP 5.3
Jeremy Coates
 
What is new in PHP
Haim Michael
 
Introducing PHP Latest Updates
Iftekhar Eather
 
20 cool features that is in PHP 7, we missed in PHP 5. Let walkthrough with t...
DrupalMumbai
 
Php 5.4: New Language Features You Will Find Useful
David Engel
 
Practical PHP 5.3
Nate Abele
 

Recently uploaded (20)

PDF
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
PDF
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
PPTX
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PPTX
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PPTX
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PDF
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PPTX
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
PDF
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PPTX
Platform for Enterprise Solution - Java EE5
abhishekoza1981
 
PPTX
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
Platform for Enterprise Solution - Java EE5
abhishekoza1981
 
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 

Hack programming language

  • 2. Agenda  What is the Hack Language ?  Hack Modes  Type Annotations  Nullable  Generics  Collections  Type Aliasing  Constructor Argument Promotion  PHP to Hack Conversion DEMO
  • 3. Not on Agenda, but interesting  Async - asynchronous programming support via async/await  XHP – an extension of the PHP and Hack syntax where XML elements/blocks become valid expressions  Lamba expressions – a more advanced version of PHP closures
  • 4. What is the Hack Language ?  Language for HHVM that interoperates with PHP  Developed and used by Facebook  Open-Sourced in March 2014  Basically, PHP with a ton of extra features  Hack = Fast development cycle of PHP + discipline of strong typing  Combines strong typing with weak typing => gradual typing (you can strong type only the parts you want)  Write cleaner/safer code while maintaining good compatibility with existing PHP codebases
  • 5. What is the Hack Language ? (II)  Hack code uses “<?hh” instead of “<?php”. Not mandatory but, if you do it, you won’t be able to jump between Hack and HTML anymore  Hack and PHP can coexist in the same project. Good if you want an incremental switch to Hack.  PHP code can call Hack code and vice-versa  Closing tags (“?>”) are not supported  Naming your files “*.hh” can be a good practice
  • 6. Hack Modes (I)  Intended for maximum flexibility converting PHP to Hack  Helps when running “hackificator”  Each file can have only one Hack mode  Default is “partial”  Declared at the top: “<?hh // strict”
  • 7. Hack Modes (II)  Strict:  <?hh // strict  Type checker will catch every error  All code in this mode must be correctly annotated  Code in strict mode cannot call non-Hack code.
  • 8. Hack Modes (III)  Partial:  <?hh // partial  <?hh  Default mode  Allows calling non-Hack code  Allows ommitting some function parameters
  • 9. Hack Modes (IV)  Decl:  <?hh // decl  used when annotating old APIs  Allows Hack code written in strict mode to call into legacy code. Just like in partial mode, but without having to fix reported problems
  • 10. Type Annotations (I)  Readability by helping other developers understand the purpose and intention of the code. Many used comments to do this, but Hack formalizes it instead  Correctness by forbidding unsafe coding practices  Make use of automatic and solid refactoring tools of a strong-typed language.
  • 11. Type Annotations (II)  class MyExampleClass {  public int $number;  public CustomClass $myObject;  private string $theName;  protected array $mystuff;   function doStuff(string $name, bool $withHate): string {  if ($withHate && $name === 'Satan') {  return '666 HAHA';  }  return '';  }  }
  • 12. Type Annotations (III)  Possible annotations:  Primitive types: int, string, float, bool, array  User-defined classes: MyClass, Vector<mytype>  Generics: MyClass<T>  Mixed: mixed  Void: void  Typed arrays: array<Foo>, array<string, array<string, MyClass>>  Tuples: e.g. tuple(string, bool)  XHP elements  Closures: (function(type_1, type_2): return_type)  Resources: resource  this: this
  • 13. Nullable  Safer way to deal with nulls  In type annotation, add “?” to the type to make it nullable. e.g. ?string  Normal PHP code does allow some annotation:  public function doStuff(MyClass $obj)  But passing null to this results in a fatal error  So in Hack you can do this:  public function doStuff(?MyClass $obj)  It’s best not to abuse this feature, but to use it when you really need it
  • 14. Generics (I)  Allows classes and methods to be parameterized  Generics code can be statically checked for correctness  Offers a great alternative against using a top-level object + a bunch of instanceof calls + type casts  In most languages, classes must be used as types. But Hack allows primitives too  They are immutable: once a type has been associated, it can not be changed  In Hack, objects can be used as types too  Nullable types are also supported  Interfaces and Traits support Generics too  See docs for more details
  • 15. Generics (II)  class MyClass<K> {  private K $param;  public function __construct(K $param) {  $this->param = $param;  }  public function getParameter(): K {  return $this->param;  }  }
  • 16. Collections (I)  Classes specialized for data storage and retrieval  PHP arrays are too general, they offer a “one-size-fits-all” approach  In both Hack and PHP, arrays are not objects. But Collections are.  Seeing the keyword "array" in a piece of code doesn't make it clear how that array will be used  But each Collection class is best suited for specific situations.  So code is clearer and, if used correctly, can be a performance improvement
  • 17. Collections (II)  They are traversable with “foreach” loops  Support bracket notations: $mycollection[$index]  Since they are objects, they are not copied when passed around as parameters  They integrate and work very well with Generics  Immutable variants of the Collection classes also exist. This is to ensure that they are not changed when passed around  Classes are: Vector, Map, Set, Pair  Immutable collections: ImmVector, ImmMap, ImmSet
  • 18. Collections (III)  Vector example:  $vector = Vector {6, 5, 4, 9};  $vector->add(97);  $vector->add(31);  $vector->get(1);  $x = $vector[2];  $vector->removeKey(2);  $vector[] = 999;  foreach ($vector as $key => $value) {...}
  • 19. Collections (IV)  Map example:  $map = Map {“key1" => 1, “key2" => 2, “key3" => 3};  $b = $map->get(“key2");  $map->remove(“key2");  $map->contains(“key1");  foreach ($map as $key => $value) {...}
  • 20. Type Aliasing  Just like PHP has aliasing support in namespaces, so does Hack provide the same functionality for any type  Two modes of declaration:  type MyType = string;  newtype MyType = string;  The 2nd is called “opaque aliasing” and is only visible in the same file  Composite types can also be declared:  newtype Coordinate = (float, float);  These are implemented as tuples, so that is how they must be created in order to be used
  • 21. Constructor Argument Promotion (I)  Before:  class User {  private string $name;  private string $email;  private int $age;  public function __construct(string $name, string $email, int $age) {  $this->name = $name;  $this->email = $email;  $this->age = $age;  }  }
  • 22. Constructor Argument Promotion (II)  After:  class User {  public function __construct(  private string $name,  private string $email,  private int $age  ) {}  }
  • 24. Q & A

Editor's Notes

  • #3: Alteratives: simpletest, behat
  • #4: Alteratives: simpletest, behat
  • #5: Alteratives: simpletest, behat
  • #6: Alteratives: simpletest, behat
  • #7: Alteratives: simpletest, behat
  • #8: Alteratives: simpletest, behat
  • #9: Alteratives: simpletest, behat
  • #10: Alteratives: simpletest, behat
  • #11: Alteratives: simpletest, behat
  • #12: Alteratives: simpletest, behat
  • #13: Alteratives: simpletest, behat
  • #14: Alteratives: simpletest, behat
  • #15: Alteratives: simpletest, behat
  • #16: Alteratives: simpletest, behat
  • #17: Alteratives: simpletest, behat
  • #18: Alteratives: simpletest, behat
  • #19: Alteratives: simpletest, behat
  • #20: Alteratives: simpletest, behat
  • #21: Alteratives: simpletest, behat
  • #22: Alteratives: simpletest, behat
  • #23: Alteratives: simpletest, behat
  • #24: Alteratives: simpletest, behat