forked from hakimel/reveal.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1706 lines (1378 loc) · 51.8 KB
/
index.html
File metadata and controls
1706 lines (1378 loc) · 51.8 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>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Big Little Lies, Big Little Types</title>
<link rel="stylesheet" href="dist/reset.css">
<link rel="stylesheet" href="dist/reveal.css">
<link rel="stylesheet" href="dist/theme/big-little-types.css">
<!-- Theme used for syntax highlighted code -->
<!-- <link rel="stylesheet" href="plugin/highlight/monokai.css">-->
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<section>
<div class="r-stack">
<h1 class="fragment fade-out" data-fragment-index="0">
Big Little Lies,<br>
Big Little Types
</h1>
<h2 class="fix-heading-alignment fragment" data-fragment-index="0">
Making React More Type Safe With Type-Tagging in Scala.js
</h2>
</div>
<div class="r-stack">
<img class="hero" data-src="assets/big-little-lies.jpg">
<img class="logo fragment" data-fragment-index="0" data-src="assets/react-icon.svg"
style="position: relative; left: -18%;">
<img class="logo fragment" data-fragment-index="0" data-src="assets/scala-js-logo.svg"
style="position: relative; left: 2%;">
<img class="logo fragment" data-fragment-index="0" data-src="assets/slinky-logo.svg"
style="position: relative; top: 5%; left: 19%;">
</div>
</section>
<section>
<h2>Type-Tagging</h2>
<ul>
<li>Tag a type with another type</li>
<li class="fragment">Augment the Slinky/React API</li>
<li class="fragment">Use that other type to enforce rules at compile time</li>
<li class="fragment">Make illegal states and values unrepresentable</li>
<li class="fragment">Enforce order of function calls</li>
</ul>
</section>
<section>
<h2>A Bit About Me</h2>
<ul>
<li>
Jason Pickens (
<img class="inline" data-src="assets/github-mark.svg">
<a href="https://github.com/steinybot">@steinybot</a>
)
</li>
<li class="fragment">
<div>
Father of 4 (2 human)
<div>
<img class="fragment fade-out" data-src="assets/babies.jpg"
style="height: 400px; position: absolute;">
</div>
</div>
</li>
<li class="fragment">Graduate at Orion Health in 2010</li>
<li class="fragment">First intro to FP and Scala in 2013</li>
<li class="fragment">Did a stint at Lightbend</li>
<li class="fragment">Went to dinner with the Scala team</li>
<li class="fragment">Now at Goodcover</li>
<li class="fragment">Writing Scala professionally for ~7 years</li>
</ul>
</section>
</section>
<section>
<h2>Teaser</h2>
<div class="code-overlay-container r-stack">
<pre><code data-trim data-line-numbers=""><script type="text/template">
import { ReactElement } from "react";
function RedList(props: { children: ReactElement[] }) {
return <ul style={{ color: "red" }}>{props.children}</ul>;
}
function QuantifiedListItem(props: { amount: number, children: string }) {
return <li>{props.children} * {props.amount}</li>;
}
export default function App() {
return (
<RedList>
<li>Apple</li>
<li>Banana</li>
<QuantifiedListItem amount={5}>Cherries</QuantifiedListItem>
</RedList>
);
}
</script></code></pre>
<div class="code-overlay fragment r-frame">
<ul style="color: red;">
<li>Apple</li>
<li>Banana</li>
<li>Cherries * 5</li>
</ul>
</div>
</div>
<aside class="notes">
<pre>
This is a little teaser of the React app we will
be fixing up today.
This is the TypeScript version.
It renders an unordered list with list items, one
of which has an amount.
</pre>
</aside>
</section>
<section>
<section>
<h2>React</h2>
<pre><code data-trim data-line-numbers="4,8,14-15|3,7,11,13,16-17|"><script type="text/template">
import { ReactElement } from "react";
function RedList(props: { children: ReactElement[] }) {
return <ul style={{ color: "red" }}>{props.children}</ul>;
}
function QuantifiedListItem(props: { amount: number, children: string }) {
return <li>{props.children} * {props.amount}</li>;
}
export default function App() {
return (
<RedList>
<li>Apple</li>
<li>Banana</li>
<QuantifiedListItem amount={5}>Cherries</QuantifiedListItem>
</RedList>
);
}
</script></code></pre>
<aside class="notes">
<pre>
1) Elements
2) Component and Props
</pre>
</aside>
</section>
</section>
<section>
<h2>Untyped Children</h2>
<div class="code-overlay-container r-stack">
<pre><code data-trim data-line-numbers="3,15"><script type="text/template">
import { ReactElement } from "react";
function RedList(props: { children: ReactElement[] }) {
return <ul style={{ color: "red" }}>{props.children}</ul>;
}
function QuantifiedListItem(props: { amount: number, children: string }) {
return <li>{props.children} * {props.amount}</li>;
}
export default function App() {
return (
<RedList>
<li>Apple</li>
<li>Banana</li>
<QuantifiedListItem amount={5}>Cherries</QuantifiedListItem>
</RedList>
);
}
</script></code></pre>
<pre class="fragment"><code data-trim data-line-numbers="3,15"><script type="text/template">
import { ReactElement } from "react";
function RedList(props: { children: ReactElement[] }) {
return <ul style={{ color: "red" }}>{props.children}</ul>;
}
function QuantifiedListItem(props: { amount: number, children: string }) {
return <li>{props.children} * {props.amount}</li>;
}
export default function App() {
return (
<RedList>
<li>Apple</li>
<p>Banana</p>
<QuantifiedListItem amount={5}>Cherries</QuantifiedListItem>
</RedList>
);
}
</script></code></pre>
<div class="code-overlay fragment r-frame">
<ul style="color: red;">
<li>Apple</li>
<p>Banana</p>
<li>Cherries * 5</li>
</ul>
</div>
</div>
<!-- <ul>https://flow.org/en/docs/react/children/#toc-only-allowing-a-specific-element-type-as-children</ul>-->
<aside class="notes">
<pre>
1) The type of children is ReactElement,
i.e. untyped.
2) Pass in any HTML element
3) Get semantically invalid HTML
</pre>
</aside>
</section>
<section>
<h2>Scala.js</h2>
<h3>Scala for the web</h3>
<ul>
<li class="fragment">Now a built-in target for the Scala compiler</li>
<li class="fragment">Outputs .sjsir and .class files</li>
<li class="fragment">Scala.js linker produces .js files</li>
<li class="fragment">Same semantics as JVM (mostly)</li>
<li class="fragment">Very good JavaScript interoperability</li>
<li class="fragment">Generate Scala.js facades from TypeScript (<a href="https://scalablytyped.org/docs/readme.html">ScalablyTyped</a>)</li>
</ul>
<aside class="notes">
<pre>
1) Enough about Typescript let's move on to
Scala.js
2)
3)
4)
5)
6) JVM semantics with JS interop?
- 2 different type hierarchies (Any and js.Any)
- 3 different implementations:
- 1) Scala types - not visible to JS
- 2) JS native types -
JS types implemented in JS with Scala.js
facade
- 3) Scala.js non-native types -
JS types implemented in Scala.js
7) ScalablyTyped
</pre>
</aside>
</section>
<section>
<section>
<h2>Slinky</h2>
<pre><code class="language-scala" data-trim data-line-numbers="1|3|5-8|10-11|13|15|17|"><script type="text/template">
val tag: WithAttrs[li.tag.type] = li("Apple")
val renderedTag: ReactElement = tag
val component: FunctionalComponent[Props] =
FunctionalComponent[Props] { props =>
li(s"${props.children} * ${props.amount}")
}
def fn(amount: Int)(children: String) =
li(s"$children * $amount")
val componentWithProps: KeyAddingStage = component(Props(5, "Cherries"))
val componentWithKey: ReactElement = componentWithProps.withKey("cherries")
val renderedComponent: ReactElement = componentWithProps
</script></code></pre>
<aside class="notes">
<pre>
Slinky is a Scala.js library for writing React
apps.
The API tries to match as close as possible to the
JS API.
</pre>
</aside>
</section>
<section>
<h2>Slinky</h2>
<pre><code class="language-scala" data-trim data-line-numbers="2,5,9,10|1,4,7,8,11|"><script type="text/template">
def RedList(children: ReactElement*) =
ul(style := js.Dynamic.literal(color = "red"))(children: _*)
def QuantifiedListItem(amount: Int)(children: String) =
li(s"$children * $amount")
def App =
RedList(
li("Apple"),
li("Banana"),
QuantifiedListItem(5)("Cherries")
)
</script></code></pre>
<aside class="notes">
<pre>
1) Here I'm highlighting the HTML elements or
"tags".
2) And these are the components and their props.
3) NOTE: It looks pretty close the TS + JSX
</pre>
</aside>
</section>
<section>
<h2>Slinky</h2>
<pre style="font-size: 0.4em;"><code class="language-scala" data-trim data-line-numbers="2,3,5,10,11,13,18,21-24|"><script type="text/template">
@react
object RedList {
case class Props(children: ReactElement*)
val component = FunctionalComponent[Props] { props =>
ul(style := js.Dynamic.literal(color = "red"))(props.children: _*)
}
}
@react
object QuantifiedListItem {
case class Props(amount: Int, children: String)
val component = FunctionalComponent[Props] { props =>
li(s"${props.children} * ${props.amount}")
}
}
@react
object App {
type Props = Unit
val component = FunctionalComponent[Props] { _ =>
RedList(
li("Apple"),
li("Banana"),
QuantifiedListItem(5)("Cherries")
)
}
}
</script></code></pre>
<aside class="notes">
<pre>
The previous slide showed the components
implemented as just functions which have some
limitations.
React doesn't see these as component and so they
can't use hooks (we will see hooks a bit later).
Here they are FunctionalComponents with case
classes for props.
Case classes are essentially record types.
That @react annotation is a macro which reduces
some boilerplate.
</pre>
</aside>
</section>
</section>
<section>
<h2>Typed Children</h2>
<div class="r-stack">
<pre><code class="language-scala" data-trim data-line-numbers="1,10"><script type="text/template">
def RedList(children: ReactElement*) =
ul(style := js.Dynamic.literal(color = "red"))(children: _*)
def QuantifiedListItem(amount: Int)(children: String) =
li(s"$children * $amount")
def App =
RedList(
li("Apple"),
li("Banana"),
QuantifiedListItem(5)("Cherries")
)
</script></code></pre>
<pre class="fragment"><code class="language-scala" data-trim data-line-numbers="1,10"><script type="text/template">
def RedList(children: ReactElement*) =
ul(style := js.Dynamic.literal(color = "red"))(children: _*)
def QuantifiedListItem(amount: Int)(children: String) =
li(s"$children * $amount")
def App =
RedList(
li("Apple"),
p("Banana"), // This compiles fine 😒
QuantifiedListItem(5)("Cherries")
)
</script></code></pre>
<pre class="fragment"><code class="language-scala" data-trim data-line-numbers="1,10"><script type="text/template">
def RedList(children: TypedReactElement[li.tag.type]*) =
ul(style := js.Dynamic.literal(color = "red"))(children: _*)
def QuantifiedListItem(amount: Int)(children: String) =
li(s"$children * $amount")
def App =
RedList(
li("Apple"),
p("Banana"), // This fails to compile 🥳
QuantifiedListItem(5)("Cherries")
)
</script></code></pre>
</div>
<aside class="notes">
<pre>
1)
Back to the problem!
We have a RedList component which takes any
ReactElement.
2) If we pass in a paragraph then it compiles.
3)
What we really want is one that only takes list
items,
like with this hypothetical API where the children
are TypedReactElements of list item tags.
Let's see how we might make this TypedReactElement.
</pre>
</aside>
</section>
<section>
<section>
<h2>Value Class</h2>
<div class="r-stack">
<pre><code class="language-scala" data-line-numbers="1">
<script type="text/template">class TypedReactElement[Result](val element: ReactElement) extends AnyVal
</script></code></pre>
<pre class="fragment"><code class="language-scala" data-line-numbers="3-5">
<script type="text/template">class TypedReactElement[Result](val element: ReactElement) extends AnyVal
object TypedReactElement {
implicit def fromTag[Result](tag: WithAttrs[Result]): TypedReactElement[Result] =
new TypedReactElement(tag)
}
</script></code></pre>
<pre class="fragment"><code class="language-scala" data-line-numbers="7-8">
<script type="text/template">class TypedReactElement[Result](val element: ReactElement) extends AnyVal
object TypedReactElement {
implicit def fromTag[Result](tag: WithAttrs[Result]): TypedReactElement[Result] =
new TypedReactElement(tag)
implicit def toReactElement(result: TypedReactElement[_]): ReactElement =
result.asInstanceOf[ReactElement]
}
</script></code></pre>
<pre class="fragment"><code class="language-scala" data-line-numbers="11-13|">
<script type="text/template">class TypedReactElement[Result](val element: ReactElement) extends AnyVal
object TypedReactElement {
implicit def fromTag[Result](tag: WithAttrs[Result]): TypedReactElement[Result] =
new TypedReactElement(tag)
implicit def toReactElement(result: TypedReactElement[_]): ReactElement =
result.asInstanceOf[ReactElement]
}
val tag: WithAttrs[li.tag.type] = li("Apple")
val renderedTypedTag: TypedReactElement[li.tag.type] = tag
</script></code></pre>
<pre class="fragment"><code class="language-scala" data-trim data-line-numbers="1,10|"><script type="text/template">
def RedList(children: TypedReactElement[li.tag.type]*) =
ul(style := js.Dynamic.literal(color = "red"))(children: _*)
def QuantifiedListItem(amount: Int)(children: String) =
li(s"$children * $amount")
def App =
RedList(
li("Apple"),
p("Banana"), // This fails to compile 🥳
QuantifiedListItem(5)("Cherries")
)
</script></code></pre>
</div>
<aside class="notes">
<pre>
1)
Let's try something simple, a value class.
A value class is a wrapper around an existing
value.
It extends AnyVal.
It is just like other value types such as Int,
Double, Boolean.
It might end up being boxed and unboxed.
NOTE: How this has the Result type parameter.
2)
We don't want to have to manually wrap
ReactElements so we create an implicit conversion
from a tag to a TypedReactElement.
The type of tag, WithAttrs already has the result
so we pass that along.
3) We also want to automatically unwrap it.
4)
Here it is in action.
NOTE: We are going from a WithAttrs to a
TypedReactElement automatically and the Result
type stays.
-------------
Now if we look back at our example we get what we
wanted.
There is no implicit conversion from
WithAttrs[paragraph] to
TypedReactElement[list item]
</pre>
</aside>
</section>
<section>
<h2>Unexpected Allocation</h2>
<pre><code class="language-plaintext" data-trim data-line-numbers="7"><script type="text/template">
49: invokevirtual #217 // Method slinky/web/html/li$.apply:(Lscala/collection/immutable/Seq;)Lscala/scalajs/js/Array;
52: invokevirtual #220 // Method Demo$TypedReactElement$.apply:(Lscala/scalajs/js/Array;)Lslinky/core/facade/ReactElement;
55: invokespecial #244 // Method Demo$TypedReactElement."<init>":(Lslinky/core/facade/ReactElement;)V
58: aastore
59: dup
60: iconst_1
61: new #10 // class Demo$TypedReactElement
</script></code></pre>
<aside class="notes">
<pre>
The downside is that if we look at the byte code
we see there is a new instance being created.
</pre>
</aside>
</section>
</section>
<section>
<h2>What About WithAttrs?</h2>
<pre><code class="language-scala" data-trim data-line-numbers="1,10"><script type="text/template">
def RedList(children: WithAttrs[li.tag.type]*) =
ul(style := js.Dynamic.literal(color = "red"))(children: _*)
def QuantifiedListItem(amount: Int)(children: String) =
li(s"$children * $amount")
def App =
RedList(
li("Apple"),
p("Banana"), // This also fails to compile 🤔
QuantifiedListItem(5)("Cherries")
)
</script></code></pre>
<aside class="notes">
<pre>
You might have picked up that if WithAttrs
already has the Result type then why not just
use that?
As you can see here this also fails to compile
which is what we want.
</pre>
</aside>
</section>
<section>
<h2>Remember KeyAddingStage?</h2>
<div class="r-stack">
<pre><code class="language-scala" data-trim data-line-numbers="5-6,14-16"><script type="text/template">
def RedList(children: TypedReactElement[li.tag.type]*) =
ul(style := js.Dynamic.literal(color = "red"))(children)
def QuantifiedListItem(amount: Int)(children: String) =
li(s"$children * $amount")
def App =
RedList(
li("Apple", key := "apple"),
li("Banana", key := "banana"),
QuantifiedListItem(5)("Cherries") // Where does the key go?
)
</script></code></pre>
<pre class="fragment"><code class="language-scala" data-trim data-line-numbers="4-10,16"><script type="text/template">
def RedList(children: TypedReactElement[li.tag.type]*) =
ul(style := js.Dynamic.literal(color = "red"))(children)
@react
object QuantifiedListItem {
case class Props(amount: Int, children: String)
val component = FunctionalComponent[Props] { props =>
li(s"${props.children} * ${props.amount}")
}
}
def App =
RedList(
li("Apple", key := "apple"),
li("Banana", key := "banana"),
QuantifiedListItem(5)("Cherries").withKey("cherries")
)
</script></code></pre>
<pre class="fragment"><code class="language-scala" data-trim data-line-numbers="1,16"><script type="text/template">
def RedList(children: TypedReactElement[li.tag.type]*) =
ul(style := js.Dynamic.literal(color = "red"))(children)
@react
object QuantifiedListItem {
case class Props(amount: Int, children: String)
val component = FunctionalComponent[Props] { props =>
li(s"${props.children} * ${props.amount}")
}
}
def App =
RedList(
li("Apple", key := "apple"),
li("Banana", key := "banana"),
QuantifiedListItem(5)("Cherries").withKey("cherries") // The type was lost 🤦
)
</script></code></pre>
</div>
<aside class="notes">
<pre>
1)
Here I've updated the example to add keys to each
of the list items.
The QuantifiedListItem is just a function
which is going to return a WithAttrs.
WithAttrs already has all its attributes and so
where does the key go?
2)
We can try and make QuatifiedListItem a
FunctionalComponent.
Remember what we get from applying props to a
FunctionalComponent?
It is a KeyAddingStage. That has a withKey method.
3)
But that ain't going to work.
KeyAddingStage doesn't know about our Result type.
withKey returns an untyped ReactElement not our
TypedReactElement.
</pre>
</aside>
</section>
<section>
<h2>TypedKeyAddingStage?</h2>
<div class="r-stack">
<pre><code class="language-scala" data-trim data-line-numbers=""><script type="text/template">
class TypedKeyAddingStage[Result](val stage: KeyAddingStage) extends AnyVal
</script></code></pre>
<pre class="fragment"><code class="language-scala" data-trim data-line-numbers=""><script type="text/template">
class TypedKeyAddingStage[Result](val stage: KeyAddingStage) extends AnyVal
// value class may not wrap another user-defined value class 😠
</script></code></pre>
</div>
<aside class="notes">
<pre>
1)
So we need the KeyAddingStage to retain the
Result type too.
How about creating a TypedKeyAddingStage?
2)
If we try the same thing with a value class then
we get this compilation error.
Ok that isn't going to work. Now what?
</pre>
</aside>
</section>
<section>
<h2>Type Alias</h2>
<div class="r-stack">
<pre><code class="language-scala" data-line-numbers="1-3">
<script type="text/template">trait Tag
type TypedKeyAddingStage[Result] = KeyAddingStage with Tag
</script></code></pre>
<pre class="fragment"><code class="language-scala" data-line-numbers="5-8">
<script type="text/template">trait Tag
type TypedKeyAddingStage[Result] = KeyAddingStage with Tag
object TypedKeyAddingStage {
def unsafe[Result](stage: KeyAddingStage) =
stage.asInstanceOf[TypedKeyAddingStage[Result]]
}
</script></code></pre>
<pre class="fragment"><code class="language-scala" data-line-numbers="10-13">
<script type="text/template">trait Tag
type TypedKeyAddingStage[Result] = KeyAddingStage with Tag
object TypedKeyAddingStage {
def unsafe[Result](stage: KeyAddingStage) =
stage.asInstanceOf[TypedKeyAddingStage[Result]]
}
val cherries: KeyAddingStage = QuantifiedListItem(5)("Cherries")
val typedCherries: TypedKeyAddingStage[li.tag.type] =
TypedKeyAddingStage.unsafe(cherries)
</script></code></pre>
<pre class="fragment"><code class="language-scala" data-line-numbers="15-18|">
<script type="text/template">trait Tag
type TypedKeyAddingStage[Result] = KeyAddingStage with Tag
object TypedKeyAddingStage {
def unsafe[Result](stage: KeyAddingStage) =
stage.asInstanceOf[TypedKeyAddingStage[Result]]
}
val cherries: KeyAddingStage = QuantifiedListItem(5)("Cherries")
val typedCherries: TypedKeyAddingStage[li.tag.type] =
TypedKeyAddingStage.unsafe(cherries)
val fruit: Seq[TypedKeyAddingStage[li.tag.type]] = Seq(typedCherries)
val firstFruit: TypedKeyAddingStage[li.tag.type] = fruit.head
</script></code></pre>
</div>
<pre class="fragment"><code class="language-plaintext" data-trim style="color: #d3423e;"><script type="text/template">
org.scalajs.linker.runtime.UndefinedBehaviorError:
java.lang.ClassCastException: object cannot be cast to slinky.core.KeyAddingStage
</script></code></pre>
<aside class="notes">
<pre>
1)
We can try something else.
Scala has a simple feature called a type alias.
We can take whatever types we like (real or not)
and give them a new name.
In this case we take the untyped KeyAddingStage
and create an intersection with a new trait
called Tag.
2)
Next we create a function call unsafe which can
take an untyped KeyAddingStage and tag it with
the Result type.
This is named unsafe because there is nothing
restricting what Result can be.
3) So far so good.
4)
5) Now we run it...
6)
And get a runtime exception.
So the cast really is unsafe.
</pre>
</aside>
</section>
<section>
<section>
<h2>Structural Refinement</h2>
<div class="r-stack">
<pre><code class="language-scala" data-line-numbers="1-3,7">
<script type="text/template">
trait Tag
type TypedKeyAddingStage[Result] = KeyAddingStage with Tag
object TypedKeyAddingStage {
def unsafe[Result](stage: KeyAddingStage) =
stage.asInstanceOf[TypedKeyAddingStage[Result]]
}
val cherries = QuantifiedListItem(5)("Cherries")
val typedCherries = TypedKeyAddingStage.unsafe(cherries)
val fruit = Seq(typedCherries)
val firstFruit = fruit.head
</script></code></pre>
<pre class="fragment"><code class="language-scala" data-line-numbers="1-3,7|">
<script type="text/template">type Base = {
type __TypedKeyAddingStage
}
trait Tag
type TypedKeyAddingStage[Result] = Base with KeyAddingStage with Tag
object TypedKeyAddingStage {
def unsafe[Result](stage: KeyAddingStage) =
stage.asInstanceOf[TypedKeyAddingStage[Result]]
}
val cherries = QuantifiedListItem(5)("Cherries")
val typedCherries = TypedKeyAddingStage.unsafe(cherries)
val fruit = Seq(typedCherries)
val firstFruit = fruit.head
</script></code></pre>
</div>
<aside class="notes">
<pre>
1)
Now for the real trick.
We take the previous type alias...
2)
And add this funny looking thing called Base.
Base is a type alias but the right hand side has
this weird block looking thing.
This is called a Structural Refinement.
In this case it is refining the Any type (no
identifier before the opening bracket) and it has
another nested type alias with some arbitrary name.
3) Now this runs without blowing up at runtime.
</pre>
</aside>
</section>
<section>
<h2>Avoiding Allocation</h2>
<pre><code class="language-diff" data-trim><script type="text/template">
14: invokevirtual #54 // Method bll/DemoSpec$TypedKeyAddingStage$.unsafe:
- // (Lscala/scalajs/js/Array;)Lscala/scalajs/js/Array;
+ // (Lscala/scalajs/js/Array;)Ljava/lang/Object;
...
31: invokeinterface #71, 1 // InterfaceMethod scala/collection/immutable/Seq.head:
// ()Ljava/lang/Object;
-36: checkcast #73 // class slinky/core/KeyAddingStage
-39: invokevirtual #77 // Method slinky/core/KeyAddingStage.slinky$core$KeyAddingStage$$args:
- // ()Lscala/scalajs/js/Array;
</script></code></pre>
<aside class="notes">
<pre>
What is really going on?
Here I'm showing a diff of the byte code before
and after adding the refinement type.
We can see that the result of the unsafe method is
a java/lang/Object not a scala/scalajs/js/Array.
It was a js.Array because that is the type of an
unboxed KeyAddingStage.
Now the compiler has given up trying to check
that our cast was safe. Yus!
</pre>
</aside>
</section>
</section>
<section>
<h2>Extension Methods</h2>
<div class="r-stack">
<pre><code class="language-scala" data-trim data-line-numbers="16"><script type="text/template">
def RedList(children: TypedReactElement[li.tag.type]*) =
ul(style := js.Dynamic.literal(color = "red"))(children)
@react
object QuantifiedListItem {
case class Props(amount: Int, children: String)
val component = FunctionalComponent[Props] { props =>
li(s"${props.children} * ${props.amount}")
}
}
def App =
RedList(
li("Apple", key := "apple"),
li("Banana", key := "banana"),
QuantifiedListItem(5)("Cherries").withKey("cherries") // The type was lost 🤦
)
</script></code></pre>
<pre class="fragment"><code class="language-scala" data-trim data-line-numbers=""><script type="text/template">
type Base = {
type __TypedKeyAddingStage
}
trait Tag
type TypedKeyAddingStage[Result] = Base with KeyAddingStage with Tag
object TypedKeyAddingStage {
def unsafe[Result](stage: KeyAddingStage) =
stage.asInstanceOf[TypedKeyAddingStage[Result]]
}
</script></code></pre>
<pre class="fragment"><code class="language-scala" data-trim data-line-numbers="7-12|"><script type="text/template">
type Base = {
type __TypedKeyAddingStage
}
trait Tag
object Tag {
implicit class Ops[Result](stage: TypedKeyAddingStage[Result]) {
@inline def withKeyTyped(key: String): TypedReactElement[Result] =
new TypedReactElement[Result](stage.asInstanceOf[KeyAddingStage].withKey(key))
}
}
type TypedKeyAddingStage[Result] = Base with KeyAddingStage with Tag
object TypedKeyAddingStage {
def unsafe[Result](stage: KeyAddingStage) =
stage.asInstanceOf[TypedKeyAddingStage[Result]]
}
</script></code></pre>
<pre class="fragment"><code class="language-scala" data-trim data-line-numbers="16"><script type="text/template">
def RedList(children: TypedReactElement[li.tag.type]*) =
ul(style := js.Dynamic.literal(color = "red"))(children)