-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
1138 lines (944 loc) · 61.1 KB
/
index.html
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"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui"><title>Scala 2 vs Scala 3 macros</title><link rel="stylesheet" href="./reveal.js/dist/reset.css"><link rel="stylesheet" href="./reveal.js/dist/reveal.css"><link rel="stylesheet" href="./reveal.js/dist/theme/black.css" id="theme"><!--This CSS is generated by the Asciidoctor reveal.js converter to further integrate AsciiDoc's existing semantic with reveal.js--><style type="text/css">.reveal div.right {
float: right
}
/* source blocks */
.reveal .listingblock.stretch > .content {
height: 100%
}
.reveal .listingblock.stretch > .content > pre {
height: 100%
}
.reveal .listingblock.stretch > .content > pre > code {
height: 100%;
max-height: 100%
}
/* auto-animate feature */
/* hide the scrollbar when auto-animating source blocks */
.reveal pre[data-auto-animate-target] {
overflow: hidden;
}
.reveal pre[data-auto-animate-target] code {
overflow: hidden;
}
/* add a min width to avoid horizontal shift on line numbers */
code.hljs .hljs-ln-line.hljs-ln-n {
min-width: 1.25em;
}
/* tables */
table {
border-collapse: collapse;
border-spacing: 0
}
table {
margin-bottom: 1.25em;
border: solid 1px #dedede
}
table thead tr th, table thead tr td, table tfoot tr th, table tfoot tr td {
padding: .5em .625em .625em;
font-size: inherit;
text-align: left
}
table tr th, table tr td {
padding: .5625em .625em;
font-size: inherit
}
table thead tr th, table tfoot tr th, table tbody tr td, table tr td, table tfoot tr td {
display: table-cell;
line-height: 1.6
}
td.tableblock > .content {
margin-bottom: 1.25em
}
td.tableblock > .content > :last-child {
margin-bottom: -1.25em
}
table.tableblock, th.tableblock, td.tableblock {
border: 0 solid #dedede
}
table.grid-all > thead > tr > .tableblock, table.grid-all > tbody > tr > .tableblock {
border-width: 0 1px 1px 0
}
table.grid-all > tfoot > tr > .tableblock {
border-width: 1px 1px 0 0
}
table.grid-cols > * > tr > .tableblock {
border-width: 0 1px 0 0
}
table.grid-rows > thead > tr > .tableblock, table.grid-rows > tbody > tr > .tableblock {
border-width: 0 0 1px
}
table.grid-rows > tfoot > tr > .tableblock {
border-width: 1px 0 0
}
table.grid-all > * > tr > .tableblock:last-child, table.grid-cols > * > tr > .tableblock:last-child {
border-right-width: 0
}
table.grid-all > tbody > tr:last-child > .tableblock, table.grid-all > thead:last-child > tr > .tableblock, table.grid-rows > tbody > tr:last-child > .tableblock, table.grid-rows > thead:last-child > tr > .tableblock {
border-bottom-width: 0
}
table.frame-all {
border-width: 1px
}
table.frame-sides {
border-width: 0 1px
}
table.frame-topbot, table.frame-ends {
border-width: 1px 0
}
.reveal table th.halign-left, .reveal table td.halign-left {
text-align: left
}
.reveal table th.halign-right, .reveal table td.halign-right {
text-align: right
}
.reveal table th.halign-center, .reveal table td.halign-center {
text-align: center
}
.reveal table th.valign-top, .reveal table td.valign-top {
vertical-align: top
}
.reveal table th.valign-bottom, .reveal table td.valign-bottom {
vertical-align: bottom
}
.reveal table th.valign-middle, .reveal table td.valign-middle {
vertical-align: middle
}
table thead th, table tfoot th {
font-weight: bold
}
tbody tr th {
display: table-cell;
line-height: 1.6
}
tbody tr th, tbody tr th p, tfoot tr th, tfoot tr th p {
font-weight: bold
}
thead {
display: table-header-group
}
.reveal table.grid-none th, .reveal table.grid-none td {
border-bottom: 0 !important
}
/* kbd macro */
kbd {
font-family: "Droid Sans Mono", "DejaVu Sans Mono", monospace;
display: inline-block;
color: rgba(0, 0, 0, .8);
font-size: .65em;
line-height: 1.45;
background: #f7f7f7;
border: 1px solid #ccc;
-webkit-border-radius: 3px;
border-radius: 3px;
-webkit-box-shadow: 0 1px 0 rgba(0, 0, 0, .2), 0 0 0 .1em white inset;
box-shadow: 0 1px 0 rgba(0, 0, 0, .2), 0 0 0 .1em #fff inset;
margin: 0 .15em;
padding: .2em .5em;
vertical-align: middle;
position: relative;
top: -.1em;
white-space: nowrap
}
.keyseq kbd:first-child {
margin-left: 0
}
.keyseq kbd:last-child {
margin-right: 0
}
/* callouts */
.conum[data-value] {
display: inline-block;
color: #fff !important;
background: rgba(0, 0, 0, .8);
-webkit-border-radius: 50%;
border-radius: 50%;
text-align: center;
font-size: .75em;
width: 1.67em;
height: 1.67em;
line-height: 1.67em;
font-family: "Open Sans", "DejaVu Sans", sans-serif;
font-style: normal;
font-weight: bold
}
.conum[data-value] * {
color: #fff !important
}
.conum[data-value] + b {
display: none
}
.conum[data-value]:after {
content: attr(data-value)
}
pre .conum[data-value] {
position: relative;
top: -.125em
}
b.conum * {
color: inherit !important
}
.conum:not([data-value]):empty {
display: none
}
/* Callout list */
.hdlist > table, .colist > table {
border: 0;
background: none
}
.hdlist > table > tbody > tr, .colist > table > tbody > tr {
background: none
}
td.hdlist1, td.hdlist2 {
vertical-align: top;
padding: 0 .625em
}
td.hdlist1 {
font-weight: bold;
padding-bottom: 1.25em
}
/* Disabled from Asciidoctor CSS because it caused callout list to go under the
* source listing when .stretch is applied (see #335)
* .literalblock+.colist,.listingblock+.colist{margin-top:-.5em} */
.colist td:not([class]):first-child {
padding: .4em .75em 0;
line-height: 1;
vertical-align: top
}
.colist td:not([class]):first-child img {
max-width: none
}
.colist td:not([class]):last-child {
padding: .25em 0
}
/* Override Asciidoctor CSS that causes issues with reveal.js features */
.reveal .hljs table {
border: 0
}
/* Callout list rows would have a bottom border with some reveal.js themes (see #335) */
.reveal .colist > table th, .reveal .colist > table td {
border-bottom: 0
}
/* Fixes line height with Highlight.js source listing when linenums enabled (see #331) */
.reveal .hljs table thead tr th, .reveal .hljs table tfoot tr th, .reveal .hljs table tbody tr td, .reveal .hljs table tr td, .reveal .hljs table tfoot tr td {
line-height: inherit
}
/* Columns layout */
.columns .slide-content {
display: flex;
}
.columns.wrap .slide-content {
flex-wrap: wrap;
}
.columns.is-vcentered .slide-content {
align-items: center;
}
.columns .slide-content > .column {
display: block;
flex-basis: 0;
flex-grow: 1;
flex-shrink: 1;
}
.columns .slide-content > .column > * {
padding: .75rem;
}
/* See #353 */
.columns.wrap .slide-content > .column {
flex-basis: auto;
}
.columns .slide-content > .column.is-full {
flex: none;
width: 100%;
}
.columns .slide-content > .column.is-four-fifths {
flex: none;
width: 80%;
}
.columns .slide-content > .column.is-three-quarters {
flex: none;
width: 75%;
}
.columns .slide-content > .column.is-two-thirds {
flex: none;
width: 66.6666%;
}
.columns .slide-content > .column.is-three-fifths {
flex: none;
width: 60%;
}
.columns .slide-content > .column.is-half {
flex: none;
width: 50%;
}
.columns .slide-content > .column.is-two-fifths {
flex: none;
width: 40%;
}
.columns .slide-content > .column.is-one-third {
flex: none;
width: 33.3333%;
}
.columns .slide-content > .column.is-one-quarter {
flex: none;
width: 25%;
}
.columns .slide-content > .column.is-one-fifth {
flex: none;
width: 20%;
}
.columns .slide-content > .column.has-text-left {
text-align: left;
}
.columns .slide-content > .column.has-text-justified {
text-align: justify;
}
.columns .slide-content > .column.has-text-right {
text-align: right;
}
.columns .slide-content > .column.has-text-left {
text-align: left;
}
.columns .slide-content > .column.has-text-justified {
text-align: justify;
}
.columns .slide-content > .column.has-text-right {
text-align: right;
}
.text-left {
text-align: left !important
}
.text-right {
text-align: right !important
}
.text-center {
text-align: center !important
}
.text-justify {
text-align: justify !important
}
.footnotes {
border-top: 1px solid rgba(0, 0, 0, 0.2);
padding: 0.5em 0 0 0;
font-size: 0.65em;
margin-top: 4em;
}
.byline {
font-size:.8em
}
ul.byline {
list-style-type: none;
}
ul.byline li + li {
margin-top: 0.25em;
}
</style><script>window.MathJax = {"tex":{"inlineMath":[["\\(", "\\)"]], "displayMath":[["\\[", "\\]"]], "processEscapes":false, "tags":"none"}, "options":{"ignoreHtmlClass":"nostem|nolatexmath"}, "asciimath":{"delimiters":[["\\$", "\\$"]]}, "loader":{"load":["input/asciimath", "output/chtml", "ui/menu"]}};</script><script async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.0/es5/tex-mml-chtml.js"></script></head><body><div class="reveal"><div class="slides"><section class="title" data-state="title"><h1>Scala 2 vs Scala 3 macros</h1><div class="preamble"><div class="paragraph"><p>Mateusz Kubuszok</p></div>
<aside class="notes"><div class="paragraph"><p>Preparations:
- open Chimney project (compiling!, 0.8.2)
- open Pipez (compiling!, 0.5.1)
- open terminal - 1 tab for examples, 1 for Chimney, 1 for Pipez
- arrange them to easily jump from one to another
- put speakers notes on laptop</p></div></aside></div></section>
<section id="_about_me"><h2>About me</h2><div class="slide-content"><div class="ulist"><ul><li class="fragment"><p>breaking things in Scala for 8+ years</p></li><li class="fragment"><p>breaking things for money for 10 years</p></li><li class="fragment"><p>breaking things for fun for 18(?) years</p></li><li class="fragment"><p>a little bit of open source - including co-authoring Chimney for 6 years now</p></li><li class="fragment"><p>blog at <a href="https://kubuszok.com">Kubuszok.com</a></p></li><li class="fragment"><p>niche <a href="https://leanpub.com/jvm-scala-book">Things you need to know about JVM (that matter in Scala)</a> ebook</p></li></ul></div>
<aside class="notes"><div class="paragraph"><p>Every presentation should start with some excuse, why you even are here.</p></div></aside></div></section>
<section id="_agenda"><h2>Agenda</h2><div class="slide-content"><div class="ulist"><ul><li class="fragment"><p>What is a macro method?</p></li><li class="fragment"><p>Expressions and types</p></li><li class="fragment"><p>Symbols</p></li><li class="fragment"><p>Examples showing similarities and differences when solving the same small problems</p></li><li class="fragment"><p>I will focus on examples that I saw while maintaining my libraries</p></li><li class="fragment"><p>I will NOT focus on macro annotations</p></li></ul></div>
<aside class="notes"><div class="paragraph"><p>We will demonstrate these concepts using Scala CLI scripts.</p></div></aside></div></section>
<section><section id="_what_is_macro_method"><h2>What is macro method?</h2><div class="slide-content"><div class="paragraph"><p>We can intuitively think that macro method is a code generator pretending to be some object’s method.</p></div><aside class="notes"><div class="paragraph"><p>We’ll explain what we mean by that in the next few examples. We’ll show how macros differ in behavior from normal method.</p></div></aside></div></section><section id="_lets_see_an_example_of_a_very_simple_macro"><h2>Let’s see an example of a very simple macro</h2><div class="slide-content"><div class="olist arabic"><ol class="arabic"><li class="fragment"><p>Calling the impl method is the only thing we are allowed to do.</p></li><li class="fragment"><p><code>Expr[A]</code> is the AST of code that represents the value of type <code>A</code>.</p></li><li class="fragment"><p>Why Scala 2 call it blackbox will be explained later.</p></li><li class="fragment"><p>Scala 3 has "global" expressions while Scala 2 use path-dependent types for them.</p></li></ol></div>
<aside class="notes"><div class="paragraph"><p>We’re start with the simplest code that returns a constant value (<code>01_simple_macro</code>).</p></div>
<div class="paragraph"><p>I can mention that expression is basically anything that computes a value.</p></div></aside></div></section><section id="_can_the_ast_generation_be_in_a_different_place_than_macro_method"><h2>Can the AST generation be in a different place than macro method?</h2><div class="slide-content"><div class="olist arabic"><ol class="arabic"><li class="fragment"><p>Impl doesn’t have to be in the same definition as unquoting - it doesn’t even have to be in the same package!</p></li><li class="fragment"><p>Impl has to be defined in a preceeding compilation unit to call site (macro referring to it can be in the same as call site).</p></li></ol></div>
<aside class="notes"><div class="paragraph"><p>Then we’re going to look at the example above but with classes (<code>02_macro_with_classes</code>).</p></div>
<div class="paragraph"><p>This is the first trope that we shouldn’t treat macros as methods - they don’t even have to be define in the same place as what unquotes them.</p></div></aside></div></section><section id="_lets_call_some_method_in_the_macro"><h2>Let’s call some method in the macro</h2><div class="slide-content"><aside class="notes"><div class="paragraph"><p>Now let’s call some method in the same object as the macro (<code>03_calling_methods</code>).</p></div>
<div class="paragraph"><p>We are ancouraged to use full qualified name in Scala 2 as it doesn’t understand context in quasiquotes.</p></div>
<div class="paragraph"><p>If we used private method, compilation would fail since external code cannot access private methods (<code>04_calling_private</code>).</p></div>
<div class="paragraph"><p>This is the first example showing why I consider macro method to be a codegen rather than a method.</p></div></aside></div></section><section id="_lets_try_printing_some_parameters"><h2>Let’s try printing some parameters</h2><div class="slide-content"><aside class="notes"><div class="paragraph"><p>At first let’s print some parameters to see how printing works (<code>05_print_param</code>).</p></div>
<div class="paragraph"><p>Then let’s try to print <code>this</code> (<code>06_print_this</code>).</p></div>
<div class="paragraph"><p>Scala 2 require the same names and positions of parametrs in macro and in called impl definition.</p></div>
<div class="paragraph"><p>Scala 2 require path-dependent type for WeakTypeTag as well, Scala 3 does not require it (like with Expr).</p></div>
<div class="paragraph"><p>Typed representations exists to be passed around (as params/returns/implicits), untyped can be actually worked with.</p></div>
<div class="paragraph"><p>Scala 2 contains a special value for what was before macro, and Scala 3 requires us to pass it explicitly.</p></div></aside></div></section><section><div class="slide-content"><table class="tableblock frame-all grid-all fragment" style="width:100%"><colgroup><col style="width:50%"><col style="width:50%"></colgroup><tbody><tr><td class="tableblock halign-left valign-top"><p class="tableblock">Scala 2</p></td><td class="tableblock halign-left valign-top"><p class="tableblock">Scala 3</p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>(c: scala.reflect.macros.blackbox.Context)</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>(using quotes: scala.quoted.Quotes)</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>c.WeakTypeTag[A]</code> (or <code>c.TypeTag[A]</code>)</p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>scala.quoted.Type[A]</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>c.Type</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>quotes.reflect.TypeRepr</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>c.Expr[A]</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>scala.quoted.Expr[A]</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>c.universe.Tree</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>quotes.reflect.Tree</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>c.prefix</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock">no counterpart</p></td></tr></table>
<aside class="notes"><div class="paragraph"><p>Scala 2 require the same names and positions of parametrs in macro and in called impl definition.</p></div>
<div class="paragraph"><p>Scala 2 require path-dependent type for WeakTypeTag as well, Scala 3 does not require it (like with Expr).</p></div>
<div class="paragraph"><p>Typed representations exists to be passed around (as params/returns/implicits), untyped can be actually worked with.</p></div>
<div class="paragraph"><p>Scala 2 contains a special value for what was before macro, and Scala 3 requires us to pass it explicitly.</p></div></aside></div></section><section><div class="slide-content"><table class="tableblock frame-all grid-all" style="width:100%"><colgroup><col style="width:50%"><col style="width:50%"></colgroup><tbody><tr><td class="tableblock halign-left valign-top"><p class="tableblock">Scala 2</p></td><td class="tableblock halign-left valign-top"><p class="tableblock">Scala 3</p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>weakTypeOf[A]: c.Type</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>TypeRepr.of[A]: TypeRepr</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>c.WeakTypeTag[A](tpe: c.Type)</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>(tpe: TypeRepr).asType.asInstanceOf[Type[A]]</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>expr.tree</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>expr.asTerm</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>c.Expr[A](tree)</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>tree.asExprOf[A]</code></p></td></tr></table>
<aside class="notes"><div class="paragraph"><p><code>WeakTypeTag</code> can only store proper types.</p></div>
<div class="paragraph"><p><code>quoted.Type</code> has <code>AnyKind</code> so it can also store type constructor.</p></div>
<div class="paragraph"><p><code>asExprOf</code> takes implicit <code>Type</code>.</p></div></aside></div></section><section><div class="slide-content"><table class="tableblock frame-all grid-all" style="width:100%"><colgroup><col style="width:50%"><col style="width:50%"></colgroup><tbody><tr><td class="tableblock halign-left valign-top"><p class="tableblock">Scala 2</p></td><td class="tableblock halign-left valign-top"><p class="tableblock">Scala 3</p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>show(expr)</code> or <code>showCode(expr)</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>expr.asTerm.show</code> or <code>expr.asTerm.show(using Printer.TreeCode)</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock">no counterpart</p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>expr.asTerm.show(using Printer.TreeAnsiCode)</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>showRaw(expr)</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>expr.asTerm.show(using Printer.TreeStrucrture)</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>weakTypeOf[A].toString</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>TypeRepr.of[A].show</code> or <code>TypeRepr.of[A].show(using Printer.TypeReprCode)</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock">no counterpart</p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>TypeRepr.of[A].show(using Printer.TypeReprAnsiCode)</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>showRaw(weakTypeOf[A])</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>TypeRepr.of[A].show(using Printer.TypeReprStructure)</code></p></td></tr></table>
<aside class="notes"><div class="paragraph"><p>At first let’s print some parameters to see how printing works (<code>05_print_param</code>).</p></div>
<div class="paragraph"><p>Then let’s try to print <code>this</code> (<code>06_print_this</code>).</p></div>
<div class="paragraph"><p>Scala 2 always carries around what was "before" dot macro method name, Scala 3 requires explicit passing of this.</p></div>
<div class="paragraph"><p>Scala 2 require path-dependent type for WeakTypeTag as well, Scala 3 does not require it (like with Expr).</p></div>
<div class="paragraph"><p>Scala 2 require the same names and positions of parametrs in macro and in called impl definition.</p></div>
<div class="paragraph"><p>Scala 2 contains a special value for what was before macro, and Scala 3 requires us to pass it explicitly.</p></div></aside></div></section><section><div class="slide-content"><table class="tableblock frame-all grid-all" style="width:100%"><colgroup><col style="width:50%"><col style="width:50%"></colgroup><tbody><tr><td class="tableblock halign-left valign-top"><p class="tableblock">Scala 2</p></td><td class="tableblock halign-left valign-top"><p class="tableblock">Scala 3</p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>c.enclosingPosition</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>Position.ofMacroExpansion</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>c.echo(pos, msg)</code> or <code>c.echo(msg)</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>report.info(msg, pos)</code> or <code>report.info(msg)</code> or <code>report.info(msg, expr)</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>c.warn(pos, msg)</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>report.warning(msg, pos)</code> or <code>report.warning(msg)</code> or <code>report.warning(msg, expr)</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>c.error(pos, msg)</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>report.error(msg, pos)</code> or <code>report.error(msg)</code> or <code>report.error(msg, expr)</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>c.abort(pos, msg)</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>report.errorAndAbort(msg, pos)</code> or <code>report.errorAndAbort(msg)</code> or <code>report.errorAndAbort(msg, expr)</code></p></td></tr></table>
<aside class="notes"><div class="paragraph"><p>Show example of <code>07_reporting</code>.</p></div>
<div class="paragraph"><p>Explain why <code>println</code> is not a good idea.</p></div></aside></div></section></section>
<section><section id="_analyzing_types"><h2>Analyzing types</h2><div class="slide-content"><div class="paragraph fragment"><p>Symbol - a reference to definition (type, class, val, var, method, parameter, binding…​).</p></div></div></section><section><div class="slide-content"><table class="tableblock frame-all grid-all fragment" style="width:100%"><colgroup><col style="width:50%"><col style="width:50%"></colgroup><tbody><tr><td class="tableblock halign-left valign-top"><p class="tableblock">Scala 2</p></td><td class="tableblock halign-left valign-top"><p class="tableblock">Scala 3</p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>(tpe: c.Type).typeSymbol</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>(repr: TypeRepr).typeSymbol</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>sym.isType</code> / <code>sym.isClass</code> / <code>sym.isModule</code> / <code>sym.isTerm</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>sym.isType</code> / <code>sym.isClassDef</code> / --- / <code>sym.isTerm</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>sym.asType</code>, <code>sym.asClass</code>, <code>sym.asModule</code>, <code>sym.asTerm</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock">only 1 kind of <code>Symbol</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>sym.asClass.primaryConstructor</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>sym.primaryConstructor</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>NoSymbol</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>Symbol.noSymbol</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>(tpe: c.Type).decls</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>sym.declaredFields</code> / <code>sym.declaredMethods</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>(tpe: c.Type).members</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>sym.fieldMembers</code> / <code>sym.methodMembers</code></p></td></tr></table>
<aside class="notes"><div class="paragraph"><p>Let’s try to see what information we can obtain from the type (<code>08_analyzing_type</code>).</p></div>
<div class="olist arabic"><ol class="arabic"><li><p>Scala 3 has no <code>isModule</code> - we need to check that something has <code>Flag.Modules</code></p></li><li><p>Scala 2 name it <code>isClass</code> and Scala 3 <code>isClassDef</code></p></li><li><p>When class nas no constructor it has a special <code>NoSymbol</code> value</p></li><li><p>Scala 2 has <code>members</code> (all definitions, inherited or declared) and <code>decls</code> (only definitions defined in the type) in <code>Type</code>, Scala 3 separated fields from methods and store them in <code>Symbol</code></p></li></ol></div>
<div class="paragraph"><p>I can explain that Symbol is basically anything which can have a name or handle to be referred to.</p></div></aside></div></section><section><div class="slide-content"><table class="tableblock frame-all grid-all fragment" style="width:100%"><colgroup><col style="width:25%"><col style="width:25%"><col style="width:25%"><col style="width:25%"></colgroup><tbody><tr><td class="tableblock halign-left valign-top"></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>typeParams</code> (Scala 2)</p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>paramLists</code> (Scala 2)</p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>paramSymss</code> (Scala 3)</p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>def method: Unit</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>List()</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>List()</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>List()</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>def method(): Unit</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>List()</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>List(List())</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>List(List())</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>def method(a: Int, b: String): Unit</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>List()</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>List(List(value a, value b))</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>List(List(val a, val b))</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>def method(a: Int)(b:String):Unit</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>List()</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>List(List(value a), List(value b))</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>List(List(val a), List(val b))</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>def method[A]: Unit</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>List(type A)</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>List()</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>List(List(type A))</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>def method[A](a: A): Unit</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>List(type A)</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>List(value a)</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>List(List(type A), List(val b))</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>extension [A](a: A) def method[b](b: B): Unit</code></p></td><td class="tableblock halign-left valign-top"></td><td class="tableblock halign-left valign-top"></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>List(List(type A), List(val a), List(type B), List(val b))</code></p></td></tr></table>
<aside class="notes"><div class="paragraph"><p>Mention SIP-47 Clause Interleaving.</p></div></aside></div></section></section>
<section id="_building_expressions"><h2>Building expressions</h2><div class="slide-content"><div class="paragraph"><p>Example:</p></div>
<div class="ulist"><ul><li class="fragment"><p>take a type of a <code>case class</code>/<code>sealed trait</code></p></li><li class="fragment"><p>try to create <code>List</code> with a value of this type</p></li><li class="fragment"><p>for <code>case class</code> create a value if all params has default value</p></li><li class="fragment"><p>for <code>sealed</code>, create all children that can be created (<code>case object</code>s lub <code>case class</code>es like above)</p></li></ul></div>
<aside class="notes"><div class="paragraph"><p>Show example (<code>10_example</code>).</p></div>
<div class="paragraph"><p>Show that while it looks ok, it doesn’t support all cases.</p></div></aside></div></section>
<section id="_skeletons_in_the_closet"><h2>Skeletons in the closet</h2><div class="slide-content"><aside class="notes"><div class="paragraph"><p>Examples:</p></div>
<div class="olist arabic"><ol class="arabic"><li><p>Scala 2’s companion object issue</p><div class="ulist"><ul><li><p>TODO: failing test</p></li><li><p>Chimney, Scala 2, <code>ProductTypesPlatform.scala:224</code></p></li></ul></div></li><li><p>Scala 2’s knownDirectChildren and incremental compiler</p><div class="ulist"><ul><li><p>TODO: failing test</p></li></ul></div></li><li><p>Weird bugs</p><div class="ulist"><ul><li><p>TODO: failing test</p></li><li><p>Pipez, Scala 2, <code>Macros.scala:115</code></p></li></ul></div></li><li><p>Scala 3’s typeSignatureIn</p><div class="ulist"><ul><li><p>Chimney, Scala 2, <code>TypesPlatform.scala:24</code></p></li><li><p>Chimney, Scala 3, <code>TypesPlatform.scala:29</code></p></li></ul></div></li><li><p>Scala 3’s public</p><div class="ulist"><ul><li><p>???</p></li></ul></div></li><li><p>Scala 3’s fresh name</p><div class="ulist"><ul><li><p>???</p></li></ul></div></li></ol></div>
<div class="paragraph"><p>Also mention that:
1. default values in case class have different names (<code>apply</code> vs <code><init></code>)
2. parameterless <code>case</code> is not <code>case object</code>
3. <code>@BeanProperty</code> difference</p></div></aside></div></section>
<section id="_other_differences"><h2>Other differences</h2><div class="slide-content"><table class="tableblock frame-all grid-all fragment" style="width:100%"><colgroup><col style="width:25%"><col style="width:25%"><col style="width:25%"><col style="width:25%"></colgroup><tbody><tr><td class="tableblock halign-left valign-top"><p class="tableblock">Scala 2</p></td><td class="tableblock halign-left valign-top"></td><td class="tableblock halign-left valign-top"><p class="tableblock">Scala 3</p></td><td class="tableblock halign-left valign-top"></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>def method = macro methodImpl</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>def methodImpl(c.blackbox.Context): c.Expr[…​]</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>inline def method = ${ methodImpla }</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>def methodImpl(using Quotes): Expr[…​]</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock"><code>def method = macro methodImpl</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>def methodImpl(c.whitebox.Context): c.Expr[…​]</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>transparent inline def method = ${ methodImpla }</code></p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><code>def methodImpl(using Quotes): Expr[…​]</code></p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock">"macro bundle"</p></td><td class="tableblock halign-left valign-top"></td><td class="tableblock halign-left valign-top"><p class="tableblock">no counterpart</p></td><td class="tableblock halign-left valign-top"></td></tr></table>
<aside class="notes"><div class="paragraph"><p>Show whitebox macros and transparent inline defs.</p></div>
<div class="paragraph"><p>Show macro bundles on Scala 2, and what Scala 3 has.</p></div></aside></div></section>
<section id="_summary"><h2>Summary</h2><div class="slide-content"><div class="ulist"><ul><li class="fragment"><p>basic concepts - typed and untyped expressions and types, AST, Symbols - are the same</p></li><li class="fragment"><p>Scala 2 APIs have more utilities, Scala 3 had more consistent utilities</p></li><li class="fragment"><p>both implementations have enough features to build upon them</p></li><li class="fragment"><p>both implementations have rather basic documentation</p></li><li class="fragment"><p>examples and slides available on my GitHub (GitHub.com/MateuszKubuszok)</p></li></ul></div></div></section>
<section id="_questions"><h2>Questions?</h2></section>
<section id="_thank_you"><h2>Thank You!</h2></section></div></div><script src="./reveal.js/dist/reveal.js"></script><script>Array.prototype.slice.call(document.querySelectorAll('.slides section')).forEach(function(slide) {
if (slide.getAttribute('data-background-color')) return;
// user needs to explicitly say he wants CSS color to override otherwise we might break custom css or theme (#226)
if (!(slide.classList.contains('canvas') || slide.classList.contains('background'))) return;
var bgColor = getComputedStyle(slide).backgroundColor;
if (bgColor !== 'rgba(0, 0, 0, 0)' && bgColor !== 'transparent') {
slide.setAttribute('data-background-color', bgColor);
slide.style.backgroundColor = 'transparent';
}
});
// More info about config & dependencies:
// - https://github.com/hakimel/reveal.js#configuration
// - https://github.com/hakimel/reveal.js#dependencies
Reveal.initialize({
// Display presentation control arrows
controls: true,
// Help the user learn the controls by providing hints, for example by
// bouncing the down arrow when they first encounter a vertical slide
controlsTutorial: true,
// Determines where controls appear, "edges" or "bottom-right"
controlsLayout: 'bottom-right',
// Visibility rule for backwards navigation arrows; "faded", "hidden"
// or "visible"
controlsBackArrows: 'faded',
// Display a presentation progress bar
progress: true,
// Display the page number of the current slide
slideNumber: false,
// Control which views the slide number displays on
showSlideNumber: 'all',
// Add the current slide number to the URL hash so that reloading the
// page/copying the URL will return you to the same slide
hash: false,
// Push each slide change to the browser history. Implies `hash: true`
history: true,
// Enable keyboard shortcuts for navigation
keyboard: true,
// Enable the slide overview mode
overview: true,
// Disables the default reveal.js slide layout so that you can use custom CSS layout
disableLayout: false,
// Vertical centering of slides
center: true,
// Enables touch navigation on devices with touch input
touch: true,
// Loop the presentation
loop: false,
// Change the presentation direction to be RTL
rtl: false,
// See https://github.com/hakimel/reveal.js/#navigation-mode
navigationMode: 'default',
// Randomizes the order of slides each time the presentation loads
shuffle: false,
// Turns fragments on and off globally
fragments: true,
// Flags whether to include the current fragment in the URL,
// so that reloading brings you to the same fragment position
fragmentInURL: true,
// Flags if the presentation is running in an embedded mode,
// i.e. contained within a limited portion of the screen
embedded: false,
// Flags if we should show a help overlay when the questionmark
// key is pressed
help: true,
// Flags if speaker notes should be visible to all viewers
showNotes: false,
// Global override for autolaying embedded media (video/audio/iframe)
// - null: Media will only autoplay if data-autoplay is present
// - true: All media will autoplay, regardless of individual setting
// - false: No media will autoplay, regardless of individual setting
autoPlayMedia: null,
// Global override for preloading lazy-loaded iframes
// - null: Iframes with data-src AND data-preload will be loaded when within
// the viewDistance, iframes with only data-src will be loaded when visible
// - true: All iframes with data-src will be loaded when within the viewDistance
// - false: All iframes with data-src will be loaded only when visible
preloadIframes: null,
// Number of milliseconds between automatically proceeding to the
// next slide, disabled when set to 0, this value can be overwritten
// by using a data-autoslide attribute on your slides
autoSlide: 0,
// Stop auto-sliding after user input
autoSlideStoppable: true,
// Use this method for navigation when auto-sliding
autoSlideMethod: Reveal.navigateNext,
// Specify the average time in seconds that you think you will spend
// presenting each slide. This is used to show a pacing timer in the
// speaker view
defaultTiming: 120,
// Specify the total time in seconds that is available to
// present. If this is set to a nonzero value, the pacing
// timer will work out the time available for each slide,
// instead of using the defaultTiming value
totalTime: 2700,
// Specify the minimum amount of time you want to allot to
// each slide, if using the totalTime calculation method. If
// the automated time allocation causes slide pacing to fall
// below this threshold, then you will see an alert in the
// speaker notes window
minimumTimePerSlide: 0,
// Enable slide navigation via mouse wheel
mouseWheel: false,
// Hide cursor if inactive
hideInactiveCursor: true,
// Time before the cursor is hidden (in ms)
hideCursorTime: 5000,
// Hides the address bar on mobile devices
hideAddressBar: true,
// Opens links in an iframe preview overlay
// Add `data-preview-link` and `data-preview-link="false"` to customise each link
// individually
previewLinks: false,
// Transition style (e.g., none, fade, slide, convex, concave, zoom)
transition: 'slide',
// Transition speed (e.g., default, fast, slow)
transitionSpeed: 'default',
// Transition style for full page slide backgrounds (e.g., none, fade, slide, convex, concave, zoom)
backgroundTransition: 'fade',
// Number of slides away from the current that are visible
viewDistance: 3,
// Number of slides away from the current that are visible on mobile
// devices. It is advisable to set this to a lower number than
// viewDistance in order to save resources.
mobileViewDistance: 3,
// Parallax background image (e.g., "'https://s3.amazonaws.com/hakim-static/reveal-js/reveal-parallax-1.jpg'")
parallaxBackgroundImage: '',
// Parallax background size in CSS syntax (e.g., "2100px 900px")
parallaxBackgroundSize: '',
// Number of pixels to move the parallax background per slide
// - Calculated automatically unless specified
// - Set to 0 to disable movement along an axis
parallaxBackgroundHorizontal: null,
parallaxBackgroundVertical: null,
// The display mode that will be used to show slides
display: 'block',
// The "normal" size of the presentation, aspect ratio will be preserved
// when the presentation is scaled to fit different resolutions. Can be
// specified using percentage units.
width: 960,
height: 700,
// Factor of the display size that should remain empty around the content
margin: 0.1,
// Bounds for smallest/largest possible scale to apply to content
minScale: 0.2,
maxScale: 1.5,
// PDF Export Options
// Put each fragment on a separate page
pdfSeparateFragments: true,
// For slides that do not fit on a page, max number of pages
pdfMaxPagesPerSlide: 1,
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: './reveal.js/plugin/zoom/zoom.js', async: true, callback: function () { Reveal.registerPlugin(RevealZoom) } },
{ src: './reveal.js/plugin/notes/notes.js', async: true, callback: function () { Reveal.registerPlugin(RevealNotes) } }
],
});</script><script>var dom = {};
dom.slides = document.querySelector('.reveal .slides');
function getRemainingHeight(element, slideElement, height) {
height = height || 0;
if (element) {
var newHeight, oldHeight = element.style.height;
// Change the .stretch element height to 0 in order find the height of all
// the other elements
element.style.height = '0px';
// In Overview mode, the parent (.slide) height is set of 700px.
// Restore it temporarily to its natural height.
slideElement.style.height = 'auto';
newHeight = height - slideElement.offsetHeight;
// Restore the old height, just in case
element.style.height = oldHeight + 'px';
// Clear the parent (.slide) height. .removeProperty works in IE9+
slideElement.style.removeProperty('height');
return newHeight;
}
return height;
}
function layoutSlideContents(width, height) {
// Handle sizing of elements with the 'stretch' class
toArray(dom.slides.querySelectorAll('section .stretch')).forEach(function (element) {
// Determine how much vertical space we can use
var limit = 5; // hard limit
var parent = element.parentNode;
while (parent.nodeName !== 'SECTION' && limit > 0) {
parent = parent.parentNode;
limit--;
}
if (limit === 0) {
// unable to find parent, aborting!
return;
}
var remainingHeight = getRemainingHeight(element, parent, height);
// Consider the aspect ratio of media elements
if (/(img|video)/gi.test(element.nodeName)) {
var nw = element.naturalWidth || element.videoWidth, nh = element.naturalHeight || element.videoHeight;
var es = Math.min(width / nw, remainingHeight / nh);
element.style.width = (nw * es) + 'px';
element.style.height = (nh * es) + 'px';
} else {
element.style.width = width + 'px';
element.style.height = remainingHeight + 'px';
}
});
}
function toArray(o) {
return Array.prototype.slice.call(o);
}
Reveal.addEventListener('slidechanged', function () {
layoutSlideContents(960, 700)
});
Reveal.addEventListener('ready', function () {
layoutSlideContents(960, 700)
});
Reveal.addEventListener('resize', function () {
layoutSlideContents(960, 700)
});</script><link rel="stylesheet" href="./highlightjs/css/zenburn.css"/>
<script src="./highlightjs/highlight.min.js"></script>
<script>
/* highlightjs-line-numbers.js 2.6.0 | (C) 2018 Yauheni Pakala | MIT License | github.com/wcoder/highlightjs-line-numbers.js */
/* Edited by Hakim for reveal.js; removed async timeout */
!function(n,e){"use strict";function t(){var n=e.createElement("style");n.type="text/css",n.innerHTML=g(".{0}{border-collapse:collapse}.{0} td{padding:0}.{1}:before{content:attr({2})}",[v,L,b]),e.getElementsByTagName("head")[0].appendChild(n)}function r(t){"interactive"===e.readyState||"complete"===e.readyState?i(t):n.addEventListener("DOMContentLoaded",function(){i(t)})}function i(t){try{var r=e.querySelectorAll("code.hljs,code.nohighlight");for(var i in r)r.hasOwnProperty(i)&&l(r[i],t)}catch(o){n.console.error("LineNumbers error: ",o)}}function l(n,e){"object"==typeof n&&f(function(){n.innerHTML=s(n,e)})}function o(n,e){if("string"==typeof n){var t=document.createElement("code");return t.innerHTML=n,s(t,e)}}function s(n,e){e=e||{singleLine:!1};var t=e.singleLine?0:1;return c(n),a(n.innerHTML,t)}function a(n,e){var t=u(n);if(""===t[t.length-1].trim()&&t.pop(),t.length>e){for(var r="",i=0,l=t.length;i<l;i++)r+=g('<tr><td class="{0}"><div class="{1} {2}" {3}="{5}"></div></td><td class="{4}"><div class="{1}">{6}</div></td></tr>',[j,m,L,b,p,i+1,t[i].length>0?t[i]:" "]);return g('<table class="{0}">{1}</table>',[v,r])}return n}function c(n){var e=n.childNodes;for(var t in e)if(e.hasOwnProperty(t)){var r=e[t];h(r.textContent)>0&&(r.childNodes.length>0?c(r):d(r.parentNode))}}function d(n){var e=n.className;if(/hljs-/.test(e)){for(var t=u(n.innerHTML),r=0,i="";r<t.length;r++){var l=t[r].length>0?t[r]:" ";i+=g('<span class="{0}">{1}</span>\n',[e,l])}n.innerHTML=i.trim()}}function u(n){return 0===n.length?[]:n.split(y)}function h(n){return(n.trim().match(y)||[]).length}function f(e){e()}function g(n,e){return n.replace(/{(\d+)}/g,function(n,t){return e[t]?e[t]:n})}var v="hljs-ln",m="hljs-ln-line",p="hljs-ln-code",j="hljs-ln-numbers",L="hljs-ln-n",b="data-line-number",y=/\r\n|\r|\n/g;n.hljs?(n.hljs.initLineNumbersOnLoad=r,n.hljs.lineNumbersBlock=l,n.hljs.lineNumbersValue=o,t()):n.console.error("highlight.js not detected!")}(window,document);
/**
* This reveal.js plugin is wrapper around the highlight.js
* syntax highlighting library.
*/
(function( root, factory ) {
if (typeof define === 'function' && define.amd) {
root.RevealHighlight = factory();
} else if( typeof exports === 'object' ) {
module.exports = factory();
} else {
// Browser globals (root is window)
root.RevealHighlight = factory();
}
}( this, function() {
// Function to perform a better "data-trim" on code snippets
// Will slice an indentation amount on each line of the snippet (amount based on the line having the lowest indentation length)
function betterTrim(snippetEl) {
// Helper functions
function trimLeft(val) {
// Adapted from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim#Polyfill
return val.replace(/^[\s\uFEFF\xA0]+/g, '');
}
function trimLineBreaks(input) {
var lines = input.split('\n');
// Trim line-breaks from the beginning
for (var i = 0; i < lines.length; i++) {
if (lines[i].trim() === '') {
lines.splice(i--, 1);
} else break;
}
// Trim line-breaks from the end
for (var i = lines.length-1; i >= 0; i--) {
if (lines[i].trim() === '') {
lines.splice(i, 1);
} else break;
}
return lines.join('\n');
}
// Main function for betterTrim()
return (function(snippetEl) {
var content = trimLineBreaks(snippetEl.innerHTML);
var lines = content.split('\n');
// Calculate the minimum amount to remove on each line start of the snippet (can be 0)
var pad = lines.reduce(function(acc, line) {
if (line.length > 0 && trimLeft(line).length > 0 && acc > line.length - trimLeft(line).length) {
return line.length - trimLeft(line).length;
}
return acc;
}, Number.POSITIVE_INFINITY);
// Slice each line with this amount
return lines.map(function(line, index) {
return line.slice(pad);
})
.join('\n');
})(snippetEl);
}
var RevealHighlight = {
HIGHLIGHT_STEP_DELIMITER: '|',
HIGHLIGHT_LINE_DELIMITER: ',',
HIGHLIGHT_LINE_RANGE_DELIMITER: '-',
init: function( reveal ) {
// Read the plugin config options and provide fallbacks
var config = Reveal.getConfig().highlight || {};
config.highlightOnLoad = typeof config.highlightOnLoad === 'boolean' ? config.highlightOnLoad : true;
config.escapeHTML = typeof config.escapeHTML === 'boolean' ? config.escapeHTML : true;
[].slice.call( reveal.getRevealElement().querySelectorAll( 'pre code' ) ).forEach( function( block ) {
block.parentNode.className = 'code-wrapper';
// Code can optionally be wrapped in script template to avoid
// HTML being parsed by the browser (i.e. when you need to
// include <, > or & in your code).
let substitute = block.querySelector( 'script[type="text/template"]' );
if( substitute ) {
// textContent handles the HTML entity escapes for us
block.textContent = substitute.innerHTML;
}
// Trim whitespace if the "data-trim" attribute is present
if( block.hasAttribute( 'data-trim' ) && typeof block.innerHTML.trim === 'function' ) {
block.innerHTML = betterTrim( block );
}
// Escape HTML tags unless the "data-noescape" attrbute is present
if( config.escapeHTML && !block.hasAttribute( 'data-noescape' )) {
block.innerHTML = block.innerHTML.replace( /</g,"<").replace(/>/g, '>' );
}
// Re-highlight when focus is lost (for contenteditable code)
block.addEventListener( 'focusout', function( event ) {
hljs.highlightElement( event.currentTarget );
}, false );
if( config.highlightOnLoad ) {
RevealHighlight.highlightBlock( block );
}
} );
// If we're printing to PDF, scroll the code highlights of
// all blocks in the deck into view at once
reveal.on( 'pdf-ready', function() {
[].slice.call( reveal.getRevealElement().querySelectorAll( 'pre code[data-line-numbers].current-fragment' ) ).forEach( function( block ) {
RevealHighlight.scrollHighlightedLineIntoView( block, {}, true );
} );
} );
},
/**
* Highlights a code block. If the <code> node has the
* 'data-line-numbers' attribute we also generate slide
* numbers.
*
* If the block contains multiple line highlight steps,
* we clone the block and create a fragment for each step.
*/
highlightBlock: function( block ) {
hljs.highlightElement( block );
// Don't generate line numbers for empty code blocks
if( block.innerHTML.trim().length === 0 ) return;
if( block.hasAttribute( 'data-line-numbers' ) ) {
hljs.lineNumbersBlock( block, { singleLine: true } );
var scrollState = { currentBlock: block };
// If there is at least one highlight step, generate
// fragments
var highlightSteps = RevealHighlight.deserializeHighlightSteps( block.getAttribute( 'data-line-numbers' ) );
if( highlightSteps.length > 1 ) {
// If the original code block has a fragment-index,
// each clone should follow in an incremental sequence
var fragmentIndex = parseInt( block.getAttribute( 'data-fragment-index' ), 10 );
if( typeof fragmentIndex !== 'number' || isNaN( fragmentIndex ) ) {
fragmentIndex = null;
}
// Generate fragments for all steps except the original block
highlightSteps.slice(1).forEach( function( highlight ) {
var fragmentBlock = block.cloneNode( true );
fragmentBlock.setAttribute( 'data-line-numbers', RevealHighlight.serializeHighlightSteps( [ highlight ] ) );
fragmentBlock.classList.add( 'fragment' );
block.parentNode.appendChild( fragmentBlock );
RevealHighlight.highlightLines( fragmentBlock );
if( typeof fragmentIndex === 'number' ) {
fragmentBlock.setAttribute( 'data-fragment-index', fragmentIndex );
fragmentIndex += 1;
}
else {
fragmentBlock.removeAttribute( 'data-fragment-index' );
}
// Scroll highlights into view as we step through them
fragmentBlock.addEventListener( 'visible', RevealHighlight.scrollHighlightedLineIntoView.bind( Plugin, fragmentBlock, scrollState ) );
fragmentBlock.addEventListener( 'hidden', RevealHighlight.scrollHighlightedLineIntoView.bind( Plugin, fragmentBlock.previousSibling, scrollState ) );
} );
block.removeAttribute( 'data-fragment-index' )
block.setAttribute( 'data-line-numbers', RevealHighlight.serializeHighlightSteps( [ highlightSteps[0] ] ) );
}
// Scroll the first highlight into view when the slide
// becomes visible. Note supported in IE11 since it lacks
// support for Element.closest.
var slide = typeof block.closest === 'function' ? block.closest( 'section:not(.stack)' ) : null;
if( slide ) {
var scrollFirstHighlightIntoView = function() {
RevealHighlight.scrollHighlightedLineIntoView( block, scrollState, true );
slide.removeEventListener( 'visible', scrollFirstHighlightIntoView );
}
slide.addEventListener( 'visible', scrollFirstHighlightIntoView );
}
RevealHighlight.highlightLines( block );
}
},
/**
* Animates scrolling to the first highlighted line
* in the given code block.
*/
scrollHighlightedLineIntoView: function( block, scrollState, skipAnimation ) {
cancelAnimationFrame( scrollState.animationFrameID );
// Match the scroll position of the currently visible
// code block
if( scrollState.currentBlock ) {
block.scrollTop = scrollState.currentBlock.scrollTop;
}
// Remember the current code block so that we can match
// its scroll position when showing/hiding fragments
scrollState.currentBlock = block;
var highlightBounds = RevealHighlight.getHighlightedLineBounds( block )
var viewportHeight = block.offsetHeight;
// Subtract padding from the viewport height
var blockStyles = getComputedStyle( block );
viewportHeight -= parseInt( blockStyles.paddingTop ) + parseInt( blockStyles.paddingBottom );
// Scroll position which centers all highlights
var startTop = block.scrollTop;
var targetTop = highlightBounds.top + ( Math.min( highlightBounds.bottom - highlightBounds.top, viewportHeight ) - viewportHeight ) / 2;
// Account for offsets in position applied to the
// <table> that holds our lines of code
var lineTable = block.querySelector( '.hljs-ln' );
if( lineTable ) targetTop += lineTable.offsetTop - parseInt( blockStyles.paddingTop );
// Make sure the scroll target is within bounds
targetTop = Math.max( Math.min( targetTop, block.scrollHeight - viewportHeight ), 0 );
if( skipAnimation === true || startTop === targetTop ) {
block.scrollTop = targetTop;
}
else {
// Don't attempt to scroll if there is no overflow
if( block.scrollHeight <= viewportHeight ) return;
var time = 0;
var animate = function() {
time = Math.min( time + 0.02, 1 );
// Update our eased scroll position
block.scrollTop = startTop + ( targetTop - startTop ) * RevealHighlight.easeInOutQuart( time );
// Keep animating unless we've reached the end
if( time < 1 ) {
scrollState.animationFrameID = requestAnimationFrame( animate );
}
};
animate();
}
},
/**
* The easing function used when scrolling.
*/
easeInOutQuart: function( t ) {
// easeInOutQuart
return t<.5 ? 8*t*t*t*t : 1-8*(--t)*t*t*t;
},
getHighlightedLineBounds: function( block ) {
var highlightedLines = block.querySelectorAll( '.highlight-line' );
if( highlightedLines.length === 0 ) {
return { top: 0, bottom: 0 };
}
else {
var firstHighlight = highlightedLines[0];
var lastHighlight = highlightedLines[ highlightedLines.length -1 ];
return {
top: firstHighlight.offsetTop,
bottom: lastHighlight.offsetTop + lastHighlight.offsetHeight
}
}
},
/**
* Visually emphasize specific lines within a code block.
* This only works on blocks with line numbering turned on.
*
* @param {HTMLElement} block a <code> block