SlideShare a Scribd company logo
1 of 25
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

Introduction to Dart
Introduction to DartIntroduction to Dart
Introduction to DartRamesh Nair
 
RESTLess Design with Apache Thrift: Experiences from Apache Airavata
RESTLess Design with Apache Thrift: Experiences from Apache AiravataRESTLess Design with Apache Thrift: Experiences from Apache Airavata
RESTLess Design with Apache Thrift: Experiences from Apache Airavatasmarru
 
An introduction to Apache Thrift
An introduction to Apache ThriftAn introduction to Apache Thrift
An introduction to Apache ThriftMike Frampton
 
Static Analysis of PHP Code – IPC Berlin 2016
Static Analysis of PHP Code – IPC Berlin 2016Static Analysis of PHP Code – IPC Berlin 2016
Static Analysis of PHP Code – IPC Berlin 2016Rouven Weßling
 
Presentation of Python, Django, DockerStack
Presentation of Python, Django, DockerStackPresentation of Python, Django, DockerStack
Presentation of Python, Django, DockerStackDavid Sanchez
 
What is the Joomla Framework and why do we need it?
What is the Joomla Framework and why do we need it?What is the Joomla Framework and why do we need it?
What is the Joomla Framework and why do we need it?Rouven Weßling
 
Introduction to web and php mysql
Introduction to web and php mysqlIntroduction to web and php mysql
Introduction to web and php mysqlProgrammer Blog
 
Go lang introduction
Go lang introductionGo lang introduction
Go lang introductionyangwm
 
Introduction to go language programming
Introduction to go language programmingIntroduction to go language programming
Introduction to go language programmingMahmoud Masih Tehrani
 
Programming with Python - Adv.
Programming with Python - Adv.Programming with Python - Adv.
Programming with Python - Adv.Mosky Liu
 

What's hot (20)

Introduction to php basics
Introduction to php   basicsIntroduction to php   basics
Introduction to php basics
 
Apache Thrift
Apache ThriftApache Thrift
Apache Thrift
 
Introduction to Dart
Introduction to DartIntroduction to Dart
Introduction to Dart
 
Introduction to php php++
Introduction to php php++Introduction to php php++
Introduction to php php++
 
Php’s guts
Php’s gutsPhp’s guts
Php’s guts
 
Unit VI
Unit VI Unit VI
Unit VI
 
RESTLess Design with Apache Thrift: Experiences from Apache Airavata
RESTLess Design with Apache Thrift: Experiences from Apache AiravataRESTLess Design with Apache Thrift: Experiences from Apache Airavata
RESTLess Design with Apache Thrift: Experiences from Apache Airavata
 
An introduction to Apache Thrift
An introduction to Apache ThriftAn introduction to Apache Thrift
An introduction to Apache Thrift
 
PHP7 is coming
PHP7 is comingPHP7 is coming
PHP7 is coming
 
Static Analysis of PHP Code – IPC Berlin 2016
Static Analysis of PHP Code – IPC Berlin 2016Static Analysis of PHP Code – IPC Berlin 2016
Static Analysis of PHP Code – IPC Berlin 2016
 
Programming in hack
Programming in hackProgramming in hack
Programming in hack
 
Presentation of Python, Django, DockerStack
Presentation of Python, Django, DockerStackPresentation of Python, Django, DockerStack
Presentation of Python, Django, DockerStack
 
What is the Joomla Framework and why do we need it?
What is the Joomla Framework and why do we need it?What is the Joomla Framework and why do we need it?
What is the Joomla Framework and why do we need it?
 
Introduction to web and php mysql
Introduction to web and php mysqlIntroduction to web and php mysql
Introduction to web and php mysql
 
50 shades of PHP
50 shades of PHP50 shades of PHP
50 shades of PHP
 
PHP 5.3
PHP 5.3PHP 5.3
PHP 5.3
 
Go lang introduction
Go lang introductionGo lang introduction
Go lang introduction
 
Introduction to go language programming
Introduction to go language programmingIntroduction to go language programming
Introduction to go language programming
 
C++ basics
C++ basicsC++ basics
C++ basics
 
Programming with Python - Adv.
Programming with Python - Adv.Programming with Python - Adv.
Programming with Python - Adv.
 

Viewers also liked

Ruby on Rails 5: Top 5 Features
Ruby on Rails 5: Top 5 FeaturesRuby on Rails 5: Top 5 Features
Ruby on Rails 5: Top 5 FeaturesPhraseApp
 
Rails 5 subjective overview
Rails 5 subjective overviewRails 5 subjective overview
Rails 5 subjective overviewJan Berdajs
 
Final web 2.0
 Final web 2.0 Final web 2.0
Final web 2.0jmdlaxman
 
FutureWorld. Real Time Everything.
FutureWorld. Real Time Everything.FutureWorld. Real Time Everything.
FutureWorld. Real Time Everything.Andy Hadfield
 
Weather17th 20th
Weather17th 20thWeather17th 20th
Weather17th 20thamykay16
 
Weather 12th 16th
Weather 12th 16thWeather 12th 16th
Weather 12th 16thamykay16
 
Compleañossiahm
CompleañossiahmCompleañossiahm
Compleañossiahmpazpau
 
Conditionals 100819134225-phpapp01
Conditionals 100819134225-phpapp01Conditionals 100819134225-phpapp01
Conditionals 100819134225-phpapp01wil_4158
 
Obscene Gestures
Obscene GesturesObscene Gestures
Obscene Gesturestathurma
 
Collaborative Filtering Survey
Collaborative Filtering SurveyCollaborative Filtering Survey
Collaborative Filtering Surveymobilizer1000
 
Web 1.0,2.0 y 3.0
Web 1.0,2.0 y 3.0Web 1.0,2.0 y 3.0
Web 1.0,2.0 y 3.0rocanela
 
Path To Prosperity Small To Midsize Companies
Path To Prosperity Small To Midsize CompaniesPath To Prosperity Small To Midsize Companies
Path To Prosperity Small To Midsize Companiesstephej2
 
Gravação do Clip "Promessa"
Gravação do Clip "Promessa"Gravação do Clip "Promessa"
Gravação do Clip "Promessa"Rebeca Vargas
 

Viewers also liked (20)

Hacking ppt
Hacking pptHacking ppt
Hacking ppt
 
Ruby on Rails 5: Top 5 Features
Ruby on Rails 5: Top 5 FeaturesRuby on Rails 5: Top 5 Features
Ruby on Rails 5: Top 5 Features
 
Git Tutorial
Git TutorialGit Tutorial
Git Tutorial
 
Rails 5 subjective overview
Rails 5 subjective overviewRails 5 subjective overview
Rails 5 subjective overview
 
Final web 2.0
 Final web 2.0 Final web 2.0
Final web 2.0
 
Favorite Author
Favorite AuthorFavorite Author
Favorite Author
 
FutureWorld. Real Time Everything.
FutureWorld. Real Time Everything.FutureWorld. Real Time Everything.
FutureWorld. Real Time Everything.
 
Weather17th 20th
Weather17th 20thWeather17th 20th
Weather17th 20th
 
Weather 12th 16th
Weather 12th 16thWeather 12th 16th
Weather 12th 16th
 
Compleañossiahm
CompleañossiahmCompleañossiahm
Compleañossiahm
 
Conditionals 100819134225-phpapp01
Conditionals 100819134225-phpapp01Conditionals 100819134225-phpapp01
Conditionals 100819134225-phpapp01
 
Obscene Gestures
Obscene GesturesObscene Gestures
Obscene Gestures
 
Building your Personal Brand
Building your Personal BrandBuilding your Personal Brand
Building your Personal Brand
 
Twitter
TwitterTwitter
Twitter
 
Collaborative Filtering Survey
Collaborative Filtering SurveyCollaborative Filtering Survey
Collaborative Filtering Survey
 
Web 1.0,2.0 y 3.0
Web 1.0,2.0 y 3.0Web 1.0,2.0 y 3.0
Web 1.0,2.0 y 3.0
 
Stop motion
Stop motionStop motion
Stop motion
 
Path To Prosperity Small To Midsize Companies
Path To Prosperity Small To Midsize CompaniesPath To Prosperity Small To Midsize Companies
Path To Prosperity Small To Midsize Companies
 
The Competitive Advantage in the Changing Digital Landscape
The Competitive Advantage in the Changing Digital Landscape The Competitive Advantage in the Changing Digital Landscape
The Competitive Advantage in the Changing Digital Landscape
 
Gravação do Clip "Promessa"
Gravação do Clip "Promessa"Gravação do Clip "Promessa"
Gravação do Clip "Promessa"
 

Similar to Hack Programming Language

php&mysql with Ethical Hacking
php&mysql with Ethical Hackingphp&mysql with Ethical Hacking
php&mysql with Ethical HackingBCET
 
Embrace dynamic PHP
Embrace dynamic PHPEmbrace dynamic PHP
Embrace dynamic PHPPaul Houle
 
170517 damien gérard framework facebook
170517 damien gérard   framework facebook170517 damien gérard   framework facebook
170517 damien gérard framework facebookGeeks Anonymes
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial javaTpoint s
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answersAkash Gawali
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy CodeRowan Merewood
 
Patterns in Python
Patterns in PythonPatterns in Python
Patterns in Pythondn
 
PHP from soup to nuts Course Deck
PHP from soup to nuts Course DeckPHP from soup to nuts Course Deck
PHP from soup to nuts Course DeckrICh morrow
 
PHP 7 - A look at the future
PHP 7 - A look at the futurePHP 7 - A look at the future
PHP 7 - A look at the futureRadu Murzea
 
Eight simple rules to writing secure PHP programs
Eight simple rules to writing secure PHP programsEight simple rules to writing secure PHP programs
Eight simple rules to writing secure PHP programsAleksandr Yampolskiy
 
Declaration programming 2017
Declaration programming 2017Declaration programming 2017
Declaration programming 2017Marke Greene
 

Similar to Hack Programming Language (20)

PHP Reviewer
PHP ReviewerPHP Reviewer
PHP Reviewer
 
php&mysql with Ethical Hacking
php&mysql with Ethical Hackingphp&mysql with Ethical Hacking
php&mysql with Ethical Hacking
 
Embrace dynamic PHP
Embrace dynamic PHPEmbrace dynamic PHP
Embrace dynamic PHP
 
170517 damien gérard framework facebook
170517 damien gérard   framework facebook170517 damien gérard   framework facebook
170517 damien gérard framework facebook
 
What is c language
What is c languageWhat is c language
What is c language
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
 
lab4_php
lab4_phplab4_php
lab4_php
 
lab4_php
lab4_phplab4_php
lab4_php
 
Patterns in Python
Patterns in PythonPatterns in Python
Patterns in Python
 
Clean Code
Clean CodeClean Code
Clean Code
 
Php1
Php1Php1
Php1
 
PHP from soup to nuts Course Deck
PHP from soup to nuts Course DeckPHP from soup to nuts Course Deck
PHP from soup to nuts Course Deck
 
PHP 7 - A look at the future
PHP 7 - A look at the futurePHP 7 - A look at the future
PHP 7 - A look at the future
 
PHP
PHPPHP
PHP
 
Xtext Webinar
Xtext WebinarXtext Webinar
Xtext Webinar
 
Eight simple rules to writing secure PHP programs
Eight simple rules to writing secure PHP programsEight simple rules to writing secure PHP programs
Eight simple rules to writing secure PHP programs
 
Declaration programming 2017
Declaration programming 2017Declaration programming 2017
Declaration programming 2017
 
Clean code
Clean codeClean code
Clean code
 

Recently uploaded

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 

Recently uploaded (20)

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 

Hack Programming Language

  • 1.
  • 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