Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Programming in hack

211 views

Published on

Slides for this talk: http://www.phpconference.nl/schedule#conference-day-1/programming-hack

Published in: Software
  • Be the first to comment

  • Be the first to like this

Programming in hack

  1. 1. Programming in Hack Alejandro Marcu Dutch PHP Conference 2016
  2. 2. 2  Started programming Logo at 8 years old  Then moved to Basic, Turbo Pascal, C++, Java  2001 – 2004 Various programming jobs in Argentina  2004 – 2008: TopCoder  2009 – 2015: Facebook Alejandro Marcu
  3. 3. 3 1. Introduction 2. Hack Types 3. Collections 4. Async 5. XHP 6. Hack Codegen What You Will Learn Today
  4. 4. Introduction
  5. 5. 5 HHVM history  2007 - Hackathon project: convert PHP to c++  2009 – HipHop (HPHPc) in prod in Facebook  2010 – Open sourced HipHop for PHP  2010 – Started the HHVM project  2013 – HHVM in prod in Facebook  2013 – HHVM open sourced
  6. 6. 6  In 2014 Wikipedia migrated to HHVM:  CPU load went from 50% to 10%  Mean page save time went 6s to 3s  Average page load time went from 1.3s to 0.9s  PHP 7 performance is close to HHVM Example of migration to HHVM
  7. 7. 7  Started in 2012 as “strict-mode” project  Grew to be much more than that  Open sourced in 2014  As of now, HHVM is the only execution engine that runs Hack  Totally interoperable with PHP, allowing for a gradual migration Hack
  8. 8. 8  Analyzes Hack programs statically  Monitors files changes in the background  Extremely fast, even in huge code bases  Can even be used by IDEs to autocomplete code Typechecker
  9. 9. Typechecker hacklang.org/tutorial/
  10. 10. 10  Type annotations  Collections  Async  XHP  Attributes  Constructor parameter Promotion  Enums  Generics Hack only features  Operators  Lambda  Null safe  XHP attribute access  Pipe operator  Shapes  Trait and interfaces requirements  Type aliases
  11. 11. 11  References  Global statements  Top level code  Variable variables  Dynamic properties  Pseudo-functions  isset  empty  Unset  goto  Etc.. Unsupported features in Hack
  12. 12. 12 <?hh //strict  Type annotations required  Can’t call PHP  No top level code  No references <?hh // partial or <?hh  The following is allowed but not checked: • Calls to PHP • Top level code • References  Type annotations not required but checked if present <?hh // decl  Not checked at all  Functions and classes are indexed  Use it only as the first step to migrate to Hack Typechecker modes
  13. 13. Type annotations
  14. 14. 14 Types  Primitive types  bool  int  float  string  array  resource  Combined primitive types  num (int or float)  arraykey (int or string)  Object types  Name of classes or interfaces
  15. 15. Return types void can be used only as a return type
  16. 16. Parameters
  17. 17. Nullable parameters
  18. 18. Local variables
  19. 19. Class attributes
  20. 20. Class attributes
  21. 21. Class attributes
  22. 22. Constructor argument promotion
  23. 23. Enums
  24. 24. Enums
  25. 25. Enums
  26. 26. Generics
  27. 27. Arrays with generics
  28. 28. Collections
  29. 29. Vector 0-indexed list Map Keys: int or str. Values: anything Set Unique elements Collections overview
  30. 30. Vector
  31. 31. Map
  32. 32. Set
  33. 33. Immutability
  34. 34. Immutability
  35. 35. Immutability
  36. 36. Async
  37. 37. 37  Single thread blocking  Single thread non-blocking  Multithread Threads
  38. 38. Single Thread blocking example
  39. 39. Single Thread blocking example
  40. 40. Single Thread blocking example
  41. 41. Async example
  42. 42. Async example
  43. 43. Async example
  44. 44. 44  Use the keyword async before function  The return type must be Awaitable<…>  Call async from async using await  Call async from non-async using HHAsiojoin  Make parallel calls using helpers in HHAsio (such as v,vm,m,mf, etc.) Async
  45. 45. XHP
  46. 46. 46 What is XHP?  XHP is used to generate HTML in PHP or Hack  No templating  No string concatenating  Represent HTML as a tree of objects  Familiar XML syntax  Secure  Strong validations  Extensible
  47. 47. Hello World composer.json xhp.php
  48. 48. Hello World
  49. 49. Adding children
  50. 50. Custom class definition
  51. 51. Inheriting and transferring attributes
  52. 52. Async components
  53. 53. 53 XHP parser transforms it into regular Hack code:  XHP class names are replaced:  First char colon is replaced with xhp_  Other colons are replaced with two underscores  Hyphens are replaced with an underscore  E.g. :ui:nav:header is replaced with xhp_ui__nav__header XHP internals
  54. 54. 54  XHP tags are replaced with constructors. E.g. will be replaced with: XHP internals
  55. 55. Hack Codegen
  56. 56. 56 Why code generation?  Code generation: writing code that writes code  Higher level of abstraction  Generate boilerplate code  Generate code from almost static data in database  Replace usage of __call
  57. 57. 57  Easy to create files, functions, classes, methods, arrays, collections, etc  Support for partially generated files  Files can be signed  No string concatenation  No need to indent or add spaces  https://github.com/facebook/hack-codegen/  Composer: require facebook/hack-codegen Hack Codegen
  58. 58. Hack Codegen example
  59. 59. Hack Codegen example output
  60. 60. ORM code generation
  61. 61. ORM code generation
  62. 62. ORM code generation
  63. 63. ORM code generation
  64. 64. Learning Resources
  65. 65. 65 Learning resources  http://hacklang.org/ : installation, tutorial, docs  “Hack & HHVM” book
  66. 66. Contact Information amarcu@gmail.com /alejandro.marcu /alejandromarcu @AlejandroMarcu /in/alejandromarcu

×