hack programming language

25
Hack Programming Language by Radu Murzea

Upload: radu-murzea

Post on 11-May-2015

638 views

Category:

Technology


1 download

DESCRIPTION

Presentation about the Hack Programming language held at Pentalog headquarters in Cluj-Napoca in May 7th 2014.

TRANSCRIPT

Page 1: Hack Programming Language

Hack Programming Languageby Radu Murzea

Page 2: Hack Programming Language

AgendaWhat is the Hack Language ?Hack ModesType AnnotationsNullableGenericsCollectionsType AliasingConstructor Argument PromotionPHP to Hack Conversion DEMO

Page 3: Hack Programming Language

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

Page 4: Hack Programming Language

What is the Hack Language ?

Language for HHVM that interoperates with PHP

Developed and used by FacebookOpen-Sourced in March 2014Basically, PHP with a ton of extra featuresHack = Fast development cycle of PHP +

discipline of strong typingCombines 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

Page 5: Hack Programming Language

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-versaClosing tags (“?>”) are not supportedNaming your files “*.hh” can be a good

practice

Page 6: Hack Programming Language

Hack Modes (I)

Intended for maximum flexibility converting PHP to Hack

Helps when running “hackificator”Each file can have only one Hack modeDefault is “partial”Declared at the top: “<?hh // strict”

Page 7: Hack Programming Language

Hack Modes (II)

Strict:<?hh // strictType checker will catch every errorAll code in this mode must be correctly

annotatedCode in strict mode cannot call non-Hack code.

Page 8: Hack Programming Language

Hack Modes (III)

Partial:<?hh // partial<?hhDefault modeAllows calling non-Hack codeAllows ommitting some function parameters

Page 9: Hack Programming Language

Hack Modes (IV)

Decl:<?hh // declused when annotating old APIsAllows Hack code written in strict mode to call

into legacy code. Just like in partial mode, but without having to fix reported problems

Page 10: Hack Programming Language

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.

Page 11: Hack Programming 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 ''; }}

Page 12: Hack Programming Language

Type Annotations (III)

Possible annotations:Primitive types: int, string, float, bool, arrayUser-defined classes: MyClass, Vector<mytype>Generics: MyClass<T>Mixed: mixedVoid: voidTyped arrays: array<Foo>, array<string,

array<string, MyClass>>Tuples: e.g. tuple(string, bool)XHP elementsClosures: (function(type_1, type_2): return_type)Resources: resourcethis: this

Page 13: Hack Programming Language

Nullable

Safer way to deal with nullsIn type annotation, add “?” to the type to make

it nullable. e.g. ?stringNormal PHP code does allow some annotation:

public function doStuff(MyClass $obj)But passing null to this results in a fatal errorSo 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

Page 14: Hack Programming Language

Generics (I)

Allows classes and methods to be parameterizedGenerics code can be statically checked for

correctnessOffers a great alternative against using a top-level

object + a bunch of instanceof calls + type castsIn most languages, classes must be used as types.

But Hack allows primitives tooThey are immutable: once a type has been

associated, it can not be changedIn Hack, objects can be used as types tooNullable types are also supportedInterfaces and Traits support Generics tooSee docs for more details

Page 15: Hack Programming Language

Generics (II)

class MyClass<K> { private K $param; public function __construct(K $param) { $this->param = $param; } public function getParameter(): K { return $this->param; }}

Page 16: Hack Programming Language

Collections (I)

Classes specialized for data storage and retrievalPHP arrays are too general, they offer a “one-size-

fits-all” approachIn 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 usedBut each Collection class is best suited for

specific situations.So code is clearer and, if used correctly, can be a

performance improvement

Page 17: Hack Programming Language

Collections (II)

They are traversable with “foreach” loopsSupport bracket notations: $mycollection[$index]Since they are objects, they are not copied when

passed around as parametersThey integrate and work very well with GenericsImmutable variants of the Collection classes

also exist. This is to ensure that they are not changed when passed around

Classes are: Vector, Map, Set, PairImmutable collections: ImmVector, ImmMap,

ImmSet

Page 18: Hack Programming Language

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) {...}

Page 19: Hack Programming Language

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) {...}

Page 20: Hack Programming Language

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

Page 21: Hack Programming Language

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; }}

Page 22: Hack Programming Language

Constructor Argument Promotion (II)

After:class User { public function __construct( private string $name, private string $email, private int $age ) {}}

Page 23: Hack Programming Language

PHP to Hack Conversion

DEMO

Page 24: Hack Programming Language

Q & A

Page 25: Hack Programming Language