-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
executable file
·1286 lines (1014 loc) · 52.2 KB
/
Copy pathindex.html
File metadata and controls
executable file
·1286 lines (1014 loc) · 52.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Brian Scaturro - Presenting Hack</title>
<meta name="description" content="An overview of Facebook's new programming language: Hack">
<meta name="author" content="Brian Scaturro">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="css/reveal.min.css">
<link rel="stylesheet" href="css/theme/night.css" id="theme">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- If the query includes 'print-pdf', include the PDF print sheet -->
<script>
if( window.location.search.match( /print-pdf/gi ) ) {
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'css/print/pdf.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
}
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<!-- Title slide -->
<section>
<h1>The Hack Programming Language</h1>
<h3>A better way to PHP</h3>
<p>
<small>By <a href="http://brianscaturro.com">Brian Scaturro</a> / <a href="https://twitter.com/scaturr">@scaturr</a></small>
</p>
</section>
<!-- Hack quote -->
<section>
<img class="no-border" alt="Hack logo" src="img/hack.jpeg" />
<blockquote>
Hack is a language for HHVM that interoperates seamlessly with PHP. The goal of Hack is to offer developers a way to write cleaner, safer and refactorable code while trying to maintain a level of compatibility with current PHP codebases.
</blockquote>
<p>
<br><a href="http://docs.hhvm.com/manual/en/hack.intro.whatis.php">http://docs.hhvm.com/manual/en/hack.intro.whatis.php</a>
</p>
</section>
<!-- Current Hack projects -->
<section>
<h2>Current Hack things in the works:</h2>
<h3>i.e - how I know things about Hack</h3>
<ul>
<li><a href="https://github.com/HackPack/HackUnit">HackUnit</a> - xUnit for Hack</li>
<li><a href="https://github.com/HackPack/Hacktions">Hacktions</a> - Event library for Hack</li>
<li><a href="https://github.com/HackPack/Hacktions">hackqueue</a> - Socket based job queue for Hack</li>
</ul>
</section>
<!-- The Type Checker -->
<section>
<section>
<h2>The Type Checker</h2>
<h4>The real value of Hack</h4>
<img alt="No errors!" src="img/type-checker-no-errors.png" />
</section>
<section>
<h3>Catch those type errors early</h3>
<img src="img/type-checker-errors.png" alt="Hack type errors" />
</section>
<section>
<h3>... even while coding</h3>
<img src="img/vim-type-checker.png" alt="type checker in vim" />
</section>
<section>
<h3>Tough type checker</h3>
<p>
The type checker is your best line of defense. However, the runtime will allow things the type checker does not approve of.
</p>
<img src="img/no-references.png" alt="Dont use references" />
</section>
<section>
<h3>... forgiving runtime</h3>
<img class="no-border" src="img/use-references.png" alt="runtime allows references" />
<img class="no-border" src="img/use-references-output.png" alt="output of reference use" />
</section>
<section>
<h3>Using hh_client</h3>
<p>
The type checker is invoked by using the <code>hh_client</code> executable that installs with Hack. This executable is currently only available on *nix platforms.
</p>
</section>
<section>
<h3>... be sure to search the right path</h3>
<p>
<code>hh_client</code> will search for types based on the location of a <code>.hhconfig</code> file. It is an empty file. Be sure to include this in the root of your projects.
</p>
<img src="img/hhconfig.png" alt="Hack project directory structure" />
</section>
</section>
<!-- Type annotation -->
<section>
<section>
<h2>Type Annotations</h2>
<blockquote>
Type annotations allow for PHP code to be explicitly typed on parameters, class member variables and return values (types are inferred for locals). These annotated types are checked via a type checker.
</blockquote>
<p>
<br><a href="http://docs.hhvm.com/manual/en/hack.annotations.php">http://docs.hhvm.com/manual/en/hack.annotations.php</a>
</p>
</section>
<section>
<h3>Typed parameters and return types</h3>
<pre>
<code data-trim contenteditable class="php">
<?hh //strict
function sum(int x, int y): int {
return x + y;
}
</code>
</pre>
</section>
<section>
<h3>Typed class members</h3>
<pre>
<code data-trim contenteditable class="php">
<?hh //strict
class TestResult
{
protected int $runCount;
protected array<Failure> $failures;
}
</code>
</pre>
</section>
<section>
<h3>Annotating closures</h3>
<pre>
<code data-trim contenteditable class="php">
<?hh //strict
function invoke((function(int $x): int) $fn): int {
return $fn(1);
}
</code>
</pre>
</section>
<section>
<h3>Annotation with $this</h3>
<pre>
<code data-trim contenteditable class="php">
<?hh //strict
class FluentObject
{
public function doThing(): this
{
//do something
return $this;
}
}
</code>
</pre>
</section>
<section>
<h3>... needs a little work yet</h3>
<img src="img/busted-this-annotation.png" alt="this annotation doesnt work so well yet" />
<p>
<a href="https://github.com/HackPack/HackUnit/blob/bug-broken-this-annotation/Runner/Options.php">https://github.com/HackPack/HackUnit/blob/bug-broken-this-annotation/Runner/Options.php</a>
</p>
</section>
<section>
<h3>... can workaround pretty easily for now</h3>
<pre>
<code data-trim contenteditable class="php">
<?hh //strict
namespace HackPack\HackUnit\Runner;
class Options
{
protected string $testPath;
/**
* Use Options type since "this" annotation is broken
* when namespaces are used
*/
public function setTestPath(string $testPath): Options
{
$this->testPath;
return $this;
}
}
</code>
</pre>
</section>
<section>
<h3>The mixed type</h3>
<p>
The Hack type checker has some pretty interesting ways of handling the "mixed" type that many PHP programmers are familiar with. The type checker will give you a pass on it if it sees you are validating the type.
</p>
</section>
<section>
<h3>... has to be checked</h3>
<pre>
<code data-trim contenteditable class="php">
<?hh
function sum(mixed $x): void {
if (is_array($x) || $x instanceof Vector) {
$s = 0;
foreach ($x as $v) {
$s += $v;
}
return $s;
}
//... do something else or throw an exception...
}
</code>
</pre>
<p>
<a href="http://docs.hhvm.com/manual/en/hack.annotations.mixedtypes.php">http://docs.hhvm.com/manual/en/hack.annotations.mixedtypes.php</a>
</p>
</section>
<section>
<h3>Annotating generators and coroutines</h3>
<p>
PHP 5.5 introduced some features that programmers in other languages have been enjoying for years: generators and coroutines. Hack has given types to both of these.
</p>
</section>
<section>
<h3>Annotating generators</h3>
<pre>
<code data-trim contenteditable class="php">
<?hh //strict
function infiniteIterator(): Continuation<int> {
$i = 0;
while (true) {
yield ++$i;
}
}
</code>
</pre>
</section>
<section>
<h3>Annotating coroutines</h3>
<pre>
<code data-trim contenteditable class="php">
<?hh
async function f(): Awaitable<int> {
return 42;
}
async function g(): Awaitable<string> {
$f = await f();
$f++;
return 'hi test ' . $f;
}
</code>
</pre>
<p>
<a href="http://docs.hhvm.com/manual/en/hack.annotations.generators.php">http://docs.hhvm.com/manual/en/hack.annotations.generators.php</a>
</p>
</section>
</section>
<!-- Hack modes -->
<section>
<section>
<h2>Hack Modes</h2>
<p>
Hack has various levels of tolerance for it's type checker. These are known as modes, and they are triggered by comments.
</p>
</section>
<section>
<h3>Strict mode</h3>
<p>
For the pure of heart. Everything must be annotated and everything is type checked. Strict code cannot call into non Hack code.
</p>
</section>
<section>
<h3>... a strict example</h3>
<pre>
<code data-trim contenteditable class="php">
<?hh //strict
class TestCase
{
public function __construct(protected string $name)
{
}
public function setUp(): void
{
}
public function expect<T>(T $context): Expectation<T>
{
return new Expectation($context);
}
}
</code>
</pre>
</section>
<section>
<h3>... strict continued</h3>
<p>
It is worth noting that top level code cannot exist in strict mode. This makes it impossible to execute a purely strict program (there is no "main" method). Partial or UNSAFE must be used as strict entry points.
</p>
</section>
<section>
<h3>A note on hhi interfaces</h3>
<p>
Strict mode will throw "Unbound name" errors when it can't find a corresponding type (including functions). Out of the box, this also includes native PHP functions! You can imagine how frustrating this would be everytime you reach for your favorite PHP function.
</p>
<p>
<br><a href="https://github.com/facebook/hhvm/pull/2656">https://github.com/facebook/hhvm/pull/2656</a>
</p>
</section>
<section>
<h3>... hhi interfaces continued</h3>
<p>
hhi files contain interfaces for most of the PHP core library. These files basically provide type information to Hack's type checker. You only have to make sure they are on the path of the type checker. After install, these are found at <code>/usr/share/hhvm/Hack/hhi</code>. Just copy them to your project's directory and you should be good.
</p>
</section>
<section>
<h3>An example hhi</h3>
<img src="img/hhi-example.png" alt="example of hhi interface" />
</section>
<section>
<h3>Partial mode</h3>
<blockquote>
Partial mode is the default of Hack. In partial mode, the type checker checks all types other than that encompassed by an // UNSAFE comment. Partial mode also allows for the partially typing of a class, method or function (e.g., only type a subset of its arguments). And, also unlike strict mode, partial mode allows engineers to call code that has not yet been "Hack-ified" (in other words, they can call into untyped code).
</blockquote>
<p>
<br>
<a href="http://docs.hhvm.com/manual/en/hack.modes.partial.php">http://docs.hhvm.com/manual/en/hack.modes.partial.php</a>
</p>
</section>
<section>
<h3>... the safe bet for program entry</h3>
<p>
Since strict mode does not allow top level code, partial mode is the method for program entry.
</p>
<p>
<br>
<a href="https://github.com/HackPack/HackUnit/blob/master/bin/Hackunit">https://github.com/HackPack/HackUnit/blob/master/bin/Hackunit</a>
</p>
</section>
<section>
<h3>Decl mode</h3>
<blockquote>
Decl mode is used to allow Hack code written in strict mode to call into legacy code, without having to fix the issues that would be pointed out by partial mode. The type checker will "absorb" the signatures of the code, but will not type check the code. Decl is mainly used when annotating old, existing APIs (i.e., when the code does note meet Hack's stricter subset of PHP).
</blockquote>
<p>
<br>
<a href="http://docs.hhvm.com/manual/en/hack.modes.decl.php">http://docs.hhvm.com/manual/en/hack.modes.decl.php</a>
</p>
</section>
<section>
<h3>UNSAFE</h3>
<blockquote>
// UNSAFE disables the type checker from the point of unsafe declaration until the end of the current block of code (where the end of the current block generally refers to the associated ending brace (}) of which the // UNSAFE is declared).
</blockquote>
<p>
<br>
<a href="https://github.com/HackPack/HackUnit/blob/master/Runner/Loading/StandardLoader.php#L98">https://github.com/HackPack/HackUnit/blob/master/Runner/Loading/StandardLoader.php#L98</a>
</p>
</section>
</section>
<!-- generics -->
<section>
<section>
<h2>Generics</h2>
<blockquote>
Hack introduces generics to PHP (in the same vein as statically type languages such as C# and Java). Generics allow classes and methods to be parameterized (i.e., a type associated when a class is instantiated or a method is called).
</blockquote>
<p>
<br>
The benefit of course being: generics can be statically checked.
</p>
<p>
<br>
<a href="http://docs.hhvm.com/manual/en/hack.generics.php">http://docs.hhvm.com/manual/en/hack.generics.php</a>
</p>
</section>
<section>
<h3>Generic interfaces are great for design</h3>
<pre>
<code data-trim contenteditable class="php">
<?hh //strict
namespace HackPack\Hacktions;
trait EventEmitter
{
protected Map<string, Vector<(function(...): void)>> $listeners = Map {};
}
</code>
</pre>
<p>
The type checker can easily catch these sort of type constraints, and our program design is better for them.
</p>
</section>
<section>
<h3>... the dream is better than reality</h3>
<p>
While Hack generics are useful type annotations, they are nowhere near as useful as generics in Java/C#.
</p>
<p>
This mainly stems from Hack's preference for inference and the fact that a type is not a concrete thing in Hack.
</p>
</section>
<section>
<h3>Inference</h3>
<p>
The preference for inference seems like a blow to readability. The following results in a type error:
</p>
<pre>
<code data-trim content-editable class="php">
$fun = () ==> { $fn = $this->callable; $fn(); }
$this->expectCallable($fun)->toThrow<ExpectationException>();
//Tests/Core/CallableExpectationTest.php|54 col 70 error| This operator is not associative, add parentheses
</code>
</pre>
<p>
<br>
<a href="http://bit.ly/1mBUm25">http://bit.ly/1mBUm25</a>
</p>
</section>
<section>
<h3>... this is ok</h3>
<pre>
<code data-trim contenteditable class="php">
$fun = () ==> { $fn = $this->callable; $fn();};
$this->expectCallable($fun)->toThrow('\HackPack\HackUnit\Core\ExpectationException');
</code>
</pre>
<p>
This snippet also demonstrates we haven't left the "magic string" pattern of PHP. Types are not concrete things in Hack - we still have to check types against strings.
</p>
</section>
<section>
<h3>Generics don't stack</h3>
<p>
Generics are not as useful in Hack because they don't stack. What I mean by stack is explained by the following from the docs:
</p>
<blockquote>
A generic method must not collide with any existing, non-generic method name (i.e, public function swap and public function swap<T>).
</blockquote>
<p>
<br>
<a href="http://docs.hhvm.com/manual/en/Hack.generics.method.php">http://docs.hhvm.com/manual/en/Hack.generics.method.php</a>
</p>
</section>
<section>
<h3>... even if they should</h3>
<pre>
<code class="php" data-trim contenteditable>
<?hh //strict
class Cook
{
use Subject<Waiter>;
use Subject<Busboy>;
}
//throws type errors
</code>
</pre>
<p>
<br>
<a href="https://github.com/HackPack/Hacktions">https://github.com/HackPack/Hacktions</a>
</p>
</section>
<section>
<h3>Summary</h3>
<p>
Generics are really useful design tools. Their presence in Hack is a welcome addition that is not present in vanilla PHP. However, they are not as useful as they are in other languages.
</p>
</section>
</section>
<!-- Nullable types -->
<section>
<section>
<h2>Nullable Types</h2>
<blockquote>
Hack introduces a safer way to deal with nulls through a concept known as the "Nullable" type. Nullable allows any type to have null assigned and checked on it.
</blockquote>
<p>
<br>
<a href="http://docs.hhvm.com/manual/en/Hack.nullable.php">http://docs.hhvm.com/manual/en/Hack.nullable.php</a>
</p>
</section>
<section>
<h3>Be explicit about the possibility of null</h3>
<pre>
<code contenteditable data-trim class="php">
<?hh //strict
class Options
{
protected ?string $HackUnitFile;
public function getHackUnitFile(): ?string
{
$path = (string) getcwd() . '/Hackunit.php';
if (! is_null($this->HackUnitFile)) {
$path = $this->HackUnitFile;
}
$path = realpath($path);
return $path ?: null;
}
}
</code>
</pre>
</section>
<section>
<h3>... be sure to check nulls</h3>
<p>
The following results in a type error:
</p>
<pre>
<code class="php" data-trim contenteditable>
<?hh //strict
class TestResult
{
protected ?float $startTime;
public function getTime(): ?float
{
$time = null;
$startTime = $this->startTime;
$time = microtime(true) - $startTime;
return $time;
}
//TestResult.php|39 col 35 error| Typing error
//TestResult.php|39 col 35 error| This is a num (int/float) because this is used in an arithmetic operation
//TestResult.php|13 col 15 error| It is incompatible with a nullable type
}
</code>
</pre>
</section>
<section>
<h3>... the following is ok</h3>
<pre>
<code class="php" contenteditable data-trim>
<?hh //strict
class TestResult
{
public function getTime(): ?float
{
$time = null;
$startTime = $this->startTime;
if (!is_null($startTime)) {
$time = microtime(true) - $startTime;
}
return $time;
}
}
</code>
</pre>
<p>
<br>
<a href="http://bit.ly/1piowdZ">http://bit.ly/1piowdZ</a>
</p>
</section>
<section>
<h3>Summary</h3>
<p>
Nullable allows you to be explicit about the possibility of null. This makes code more readable and easier to reason about.
</p>
</section>
</section>
<!-- collections -->
<section>
<section>
<h2>Collections</h2>
<p>
The goals of Hack collections are four-fold:
</p>
<ol>
<li>Provide a unified collections framework that is simple and intuitive.</li>
<li>Provide equal or better performance than the equivalent PHP array pattern.</li>
<li>Provide a collection implementation that allows for optional static typing, integrating seamlessly with Hack.
</li>
<li>Provide an easy migration path to this framework by building on top of standard functionality from PHP5.</li>
</ol>
<p>
<br>
<a href="http://docs.hhvm.com/manual/en/Hack.collections.goals.php">http://docs.hhvm.com/manual/en/Hack.collections.goals.php</a>
</p>
</section>
<section>
<h3>Vectors</h3>
<blockquote>
A Vector is an integer-indexed (zero-based) collection with similar semantics to a C++ vector or a C#/Java ArrayList. Random access to elements happen in O(1) time. Inserts occur at O(1) when added to the end, but could hit O(n) with inserts elsewhere. Removal has similar time semantics.
</blockquote>
<p>
<br>
<a href="http://docs.hhvm.com/manual/en/Hack.collections.vector.php">http://docs.hhvm.com/manual/en/Hack.collections.vector.php</a>
</p>
</section>
<section>
<h3>... an example</h3>
<p>
<a href="https://github.com/HackPack/Hacktions/blob/master/Subject.php">https://github.com/HackPack/Hacktions/blob/master/Subject.php</a>
</p>
</section>
<section>
<h3>Maps</h3>
<blockquote>
A Map is an ordered dictionary-style collection. Elements are stored as key/value pairs. Maps retain element insertion order, meaning that iterating over a Map will visit the elements in the same order that they were inserted. Insert, remove and search operations are performed in O(lg n) time or better
</blockquote>
<p>
<br>
<a href="http://docs.hhvm.com/manual/en/Hack.collections.map.php">http://docs.hhvm.com/manual/en/Hack.collections.map.php</a>
</p>
</section>
<section>
<h3>... an example</h3>
<p>
<br>
<a href="https://github.com/HackPack/Hacktions/blob/master/EventEmitter.php">https://github.com/HackPack/Hacktions/blob/master/EventEmitter.php</a>
</p>
<p>
Note: Maps only support integer and string keys for now.
</p>
</section>
<section>
<h3>Sets</h3>
<blockquote>
A Set is an ordered collection that stores unique values. Unlike vectors and maps, sets do not have keys, and thus cannot be iterated on keys.
</blockquote>
<p>
<br>
<a href="http://docs.hhvm.com/manual/en/Hack.collections.set.php">http://docs.hhvm.com/manual/en/Hack.collections.set.php</a>
</p>
</section>
<section>
<h3>... an example</h3>
<p>
<br>
<a href="http://bit.ly/1ntUqVg">http://bit.ly/1ntUqVg</a>
</p>
<p>
Note: Sets only support integer and string values for now.
</p>
</section>
<section>
<h3>Pairs</h3>
<blockquote>
A Pair is an indexed container restricted to containing exactly two elements. Pair has integer keys; key 0 refers to the first element and key 1 refers to the second element (all other integer keys are out of bounds).
</blockquote>
<p>
<br>
<a href="http://docs.hhvm.com/manual/en/Hack.collections.pair.php">http://docs.hhvm.com/manual/en/Hack.collections.pair.php</a>
</p>
</section>
<section>
<h3>... an example</h3>
<p>
<br>
<a href="http://bit.ly/1k08Rhs">http://bit.ly/1k08Rhs</a>
</p>
</section>
<section>
<h3>A note on immutable collections</h3>
<p>
Most Hack collections have immutable variants - i.e <code>ImmVector</code>. Immutable variants function like their mutable counterparts with the exception that items cannot be added or removed.
</p>
</section>
<section>
<h3>A note on arrays</h3>
<p>
Collections are now the preferred method of storing things. Arrays are still allowed, but they must be used in a new way to conform to the type checker.
</p>
<p>
<br>
<a href="http://docs.hhvm.com/manual/en/Hack.arrays.php">http://docs.hhvm.com/manual/en/Hack.arrays.php</a>
</p>
</section>
</section>
<!-- shapes -->
<section>
<section>
<h2>Shapes</h2>
<blockquote>
Since PHP does not have the concept of a structs or records, arrays are many times used to mimic a struct or record-like entity . Arrays are also used as "argument bags" to hold a bunch of arguments that will be passed to a function or method. Shapes were created to bring some structure (no pun intended) and type-checking sanity to this use case.
</blockquote>
<p>
<br>
<a href="http://docs.hhvm.com/manual/en/Hack.shapes.php">http://docs.hhvm.com/manual/en/Hack.shapes.php</a>
</p>
</section>
<section>
<h3>... an example</h3>
<p>
<br>
<a href="https://github.com/HackPack/HackUnit/blob/master/Error/TraceParser.php">https://github.com/HackPack/HackUnit/blob/master/Error/TraceParser.php</a>
</p>
</section>
</section>
<!-- type aliasing -->
<section>
<section>
<h2>Type Aliasing</h2>
<blockquote>
Many programming languages allow existing types to be redefined as new type names. The C language has typedefs. OCaml has type abbreviations. PHP even has rudimentary mechanism with its function class_alias() function.
Hack and HHVM are offering two ways to redefine type names: type aliasing and opaque type aliasing.
</blockquote>
<p>
<br>
<a href="http://docs.hhvm.com/manual/en/Hack.typealiasing.php">http://docs.hhvm.com/manual/en/Hack.typealiasing.php</a>
</p>
</section>
<section>
<h3>Type aliasing</h3>
<pre>
<code contenteditable data-trim class="php">
<?hh //strict
type Origin = shape(
'method' => string,
'message' => string,
'location' => string
);
</code>
</pre>
<p>
<br>
<a href="http://bit.ly/1tOkPwd">http://bit.ly/1tOkPwd</a>
</p>
</section>
<section>
<h3>Opaque type aliasing</h3>
<pre>
<code class="php" contenteditable data-trim>
<?hh //strict
newtype Location = shape(
'file' => string,
'line' => int
);
</code>
</pre>
<p>
Opaque type aliases work like their non-opaque counterpart with the exception that they cannot escape the confines of the file they were defined in.
<br>
<a href="http://bit.ly/1mDUWwj">http://bit.ly/1mDUWwj</a>
</p>
</section>
<section>
<h3>Mimic linear types with aliases</h3>
<pre>
<code class="php" contenteditable data-trim>
<?hh
newtype closedfile = resource;
newtype openfile = resource;
function get_file_handler(string $filename): closedfile {
return some_wrapped_function($filename);
}
function open_file_handler(closedfile $file): openfile {
$file->open();
return $file;
}
function read(openfile $file): string {
return $file->read();
}
</code>
</pre>
<p><br><a href="http://bit.ly/1irg6cD">http://bit.ly/1irg6cD</a></p>
</section>
</section>
<!-- async -->
<section>
<section>
<h2>Async</h2>
<blockquote>
Asynchronous programming refers to a programming design pattern that allows several distinct tasks to cooperatively transfer control to one another on a given thread of execution.
</blockquote>
<p><br><a href="http://docs.hhvm.com/manual/en/Hack.async.php">http://docs.hhvm.com/manual/en/Hack.async.php</a></p>
</section>
<section>
<h3>What async is not</h3>
<p>
Async is not threading. It is cooperative multitasking. Dash those ideas of easily running things in parallel.
</p>
<p>
<br>
<a href="http://bit.ly/1nTAV4e">http://bit.ly/1nTAV4e</a>
</p>
</section>
<section>
<h3>An example</h3>
<p>
<br>
<a href="https://gist.github.com/brianium/ff20fe672939de8865fa">https://gist.github.com/brianium/ff20fe672939de8865fa</a>
</p>
</section>
<section>
<h3>Summary</h3>
<p>
While not threading, this is still extremely useful, and it will only get better. See the official example on <a href="http://docs.hhvm.com/manual/en/Hack.async.fetchingexample.php">coalesced fetching.</a>
</p>
</section>
</section>