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)

DOC
php&mysql with Ethical Hacking
BCET
 
PDF
Embrace dynamic PHP
Paul Houle
 
PDF
HHVM and Hack: A quick introduction
Kuan Yen Heng
 
PDF
170517 damien gérard framework facebook
Geeks Anonymes
 
PPTX
Introduction to PHP_ Lexical structure_Array_Function_String
DeepakUlape2
 
PPTX
Why choose Hack/HHVM over PHP7
Yuji Otani
 
DOCX
What is c language
Kushaal Singla
 
PPTX
c & c++ logic building concepts practice.pptx
rawatsatish0327
 
PPTX
C programming language tutorial
javaTpoint s
 
DOC
1183 c-interview-questions-and-answers
Akash Gawali
 
PDF
Living With Legacy Code
Rowan Merewood
 
PDF
lab4_php
tutorialsruby
 
PDF
lab4_php
tutorialsruby
 
ODP
Patterns in Python
dn
 
PPTX
Clean Code
Nascenia IT
 
PPTX
PHP from soup to nuts Course Deck
rICh morrow
 
PDF
PHP Lec 2.pdfsssssssssssssssssssssssssss
ksjawyyy
 
PDF
Xtext Webinar
Heiko Behrens
 
PPTX
C_Progragramming_language_Tutorial_ppt_f.pptx
maaithilisaravanan
 
PPT
Eight simple rules to writing secure PHP programs
Aleksandr Yampolskiy
 
php&mysql with Ethical Hacking
BCET
 
Embrace dynamic PHP
Paul Houle
 
HHVM and Hack: A quick introduction
Kuan Yen Heng
 
170517 damien gérard framework facebook
Geeks Anonymes
 
Introduction to PHP_ Lexical structure_Array_Function_String
DeepakUlape2
 
Why choose Hack/HHVM over PHP7
Yuji Otani
 
What is c language
Kushaal Singla
 
c & c++ logic building concepts practice.pptx
rawatsatish0327
 
C programming language tutorial
javaTpoint s
 
1183 c-interview-questions-and-answers
Akash Gawali
 
Living With Legacy Code
Rowan Merewood
 
lab4_php
tutorialsruby
 
lab4_php
tutorialsruby
 
Patterns in Python
dn
 
Clean Code
Nascenia IT
 
PHP from soup to nuts Course Deck
rICh morrow
 
PHP Lec 2.pdfsssssssssssssssssssssssssss
ksjawyyy
 
Xtext Webinar
Heiko Behrens
 
C_Progragramming_language_Tutorial_ppt_f.pptx
maaithilisaravanan
 
Eight simple rules to writing secure PHP programs
Aleksandr Yampolskiy
 

Recently uploaded (20)

PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
PPT
MergeSortfbsjbjsfk sdfik k
RafishaikIT02044
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PDF
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
PDF
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PDF
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PPTX
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
PDF
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PDF
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
DOCX
Import Data Form Excel to Tally Services
Tally xperts
 
PPTX
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
PDF
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
PPTX
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
MergeSortfbsjbjsfk sdfik k
RafishaikIT02044
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
Human Resources Information System (HRIS)
Amity University, Patna
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
Import Data Form Excel to Tally Services
Tally xperts
 
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 

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