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
Apache Thrift
knight1128
 
PDF
Introduction to Dart
RameshNair6
 
PPT
Introduction to php php++
Tanay Kishore Mishra
 
PPTX
Php’s guts
Elizabeth Smith
 
PDF
Unit VI
Bhavsingh Maloth
 
PDF
RESTLess Design with Apache Thrift: Experiences from Apache Airavata
smarru
 
ODP
An introduction to Apache Thrift
Mike Frampton
 
PDF
PHP7 is coming
julien pauli
 
PDF
Static Analysis of PHP Code – IPC Berlin 2016
Rouven Weßling
 
PPTX
Programming in hack
Alejandro Marcu
 
PPTX
Presentation of Python, Django, DockerStack
David Sanchez
 
PDF
What is the Joomla Framework and why do we need it?
Rouven Weßling
 
PPT
Introduction to web and php mysql
Programmer Blog
 
PDF
50 shades of PHP
Maksym Hopei
 
PPTX
PHP 5.3
Chris Stone
 
PPT
Go lang introduction
yangwm
 
PDF
Introduction to go language programming
Mahmoud Masih Tehrani
 
PPTX
C++ basics
AllsoftSolutions
 
PDF
Programming with Python - Adv.
Mosky Liu
 
Apache Thrift
knight1128
 
Introduction to Dart
RameshNair6
 
Introduction to php php++
Tanay Kishore Mishra
 
Php’s guts
Elizabeth Smith
 
RESTLess Design with Apache Thrift: Experiences from Apache Airavata
smarru
 
An introduction to Apache Thrift
Mike Frampton
 
PHP7 is coming
julien pauli
 
Static Analysis of PHP Code – IPC Berlin 2016
Rouven Weßling
 
Programming in hack
Alejandro Marcu
 
Presentation of Python, Django, DockerStack
David Sanchez
 
What is the Joomla Framework and why do we need it?
Rouven Weßling
 
Introduction to web and php mysql
Programmer Blog
 
50 shades of PHP
Maksym Hopei
 
PHP 5.3
Chris Stone
 
Go lang introduction
yangwm
 
Introduction to go language programming
Mahmoud Masih Tehrani
 
C++ basics
AllsoftSolutions
 
Programming with Python - Adv.
Mosky Liu
 

Viewers also liked (20)

PPTX
Hacking ppt
giridhar_sadasivuni
 
PDF
Ruby on Rails 5: Top 5 Features
PhraseApp
 
PDF
Git Tutorial
Drake Huang
 
PDF
Rails 5 subjective overview
Jan Berdajs
 
PPTX
Final web 2.0
jmdlaxman
 
PPT
Favorite Author
jazzycherry
 
PPTX
FutureWorld. Real Time Everything.
Andy Hadfield
 
PDF
Weather17th 20th
amykay16
 
PDF
Weather 12th 16th
amykay16
 
ODP
Compleañossiahm
pazpau
 
PPT
Conditionals 100819134225-phpapp01
wil_4158
 
PPT
Obscene Gestures
tathurma
 
PPTX
Building your Personal Brand
ThinkTank Collective Intelligence Software
 
PDF
Twitter
Tia Kansara
 
PPTX
Collaborative Filtering Survey
mobilizer1000
 
DOCX
Web 1.0,2.0 y 3.0
rocanela
 
PPTX
Stop motion
jannos-89
 
PDF
Path To Prosperity Small To Midsize Companies
stephej2
 
PPTX
The Competitive Advantage in the Changing Digital Landscape
ThinkTank Collective Intelligence Software
 
PPTX
Gravação do Clip "Promessa"
Rebeca Vargas
 
Hacking ppt
giridhar_sadasivuni
 
Ruby on Rails 5: Top 5 Features
PhraseApp
 
Git Tutorial
Drake Huang
 
Rails 5 subjective overview
Jan Berdajs
 
Final web 2.0
jmdlaxman
 
Favorite Author
jazzycherry
 
FutureWorld. Real Time Everything.
Andy Hadfield
 
Weather17th 20th
amykay16
 
Weather 12th 16th
amykay16
 
Compleañossiahm
pazpau
 
Conditionals 100819134225-phpapp01
wil_4158
 
Obscene Gestures
tathurma
 
Building your Personal Brand
ThinkTank Collective Intelligence Software
 
Twitter
Tia Kansara
 
Collaborative Filtering Survey
mobilizer1000
 
Web 1.0,2.0 y 3.0
rocanela
 
Stop motion
jannos-89
 
Path To Prosperity Small To Midsize Companies
stephej2
 
The Competitive Advantage in the Changing Digital Landscape
ThinkTank Collective Intelligence Software
 
Gravação do Clip "Promessa"
Rebeca Vargas
 

Similar to Hack Programming Language (20)

PPTX
Welcome to hack
Timothy Chandler
 
PDF
Hack the Future
Jason McCreary
 
RTF
Hack language
Ravindra Bhadane
 
PDF
PHP to Hack at Slack
Scott Sandler
 
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
 
PPT
UNIT-IV WT web technology for 1st year cs
javed75
 
PDF
Preparing for the next PHP version (5.6)
Damien Seguy
 
PPT
An Elephant of a Different Colour: Hack
Vic Metcalfe
 
Welcome to hack
Timothy Chandler
 
Hack the Future
Jason McCreary
 
Hack language
Ravindra Bhadane
 
PHP to Hack at Slack
Scott Sandler
 
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
 
UNIT-IV WT web technology for 1st year cs
javed75
 
Preparing for the next PHP version (5.6)
Damien Seguy
 
An Elephant of a Different Colour: Hack
Vic Metcalfe
 

More from Radu Murzea (6)

PPTX
The World of StackOverflow
Radu Murzea
 
PPTX
SymfonyCon 2015 - A symphony of developers
Radu Murzea
 
PPTX
The World of PHP PSR Standards
Radu Murzea
 
PPTX
PHP 7 - A look at the future
Radu Murzea
 
PPTX
Hack programming language
Radu Murzea
 
PPTX
Unit Testing in PHP
Radu Murzea
 
The World of StackOverflow
Radu Murzea
 
SymfonyCon 2015 - A symphony of developers
Radu Murzea
 
The World of PHP PSR Standards
Radu Murzea
 
PHP 7 - A look at the future
Radu Murzea
 
Hack programming language
Radu Murzea
 
Unit Testing in PHP
Radu Murzea
 

Recently uploaded (20)

PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
PDF
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 

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