Debian Codegraph @

doc/learning/tutorial.html

raw
1 ---
2 layout: default
3 title: Tutorial
4 ---
5
6 <div class="hgroup">
7 <div class="hgroup-inline">
8 <div class="panel">
9 <h1 id="tutorial">Tutorial</h1>
10 </div>
11 <div style="clear: both"></div>
12 </div>
13 </div>
14
15 <div class="hgroup">
16 <div class="hgroup-inline">
17 <div class="panel">
18 <p>
19 Let's explore the various features in Jsonnet, and mix some cocktails. All example codes
20 can be edited, allowing you to experience the language interactively. If you're looking at
21 Jsonnet for the first time, this is the right place to be. For help using the
22 implementation, see <a href="getting_started.html">getting started</a>. For a detailed
23 systematic overview of the language, check out the <a
24 href="/ref/language.html">reference</a>.
25 </p>
26 </div>
27 <div style="clear: both"></div>
28 </div>
29 </div>
30
31 <div class="hgroup">
32 <div class="hgroup-inline">
33 <div class="panel">
34 <h2 id="syntax">Syntax</h2>
35 </div>
36 <div style="clear: both"></div>
37 </div>
38 </div>
39
40 <div class="hgroup">
41 <div class="hgroup-inline">
42 <div class="panel">
43 <p>
44 Any <a href="https://json.org">JSON</a> document is a valid Jsonnet program, so we'll focus
45 on what Jsonnet adds to JSON. Let's start with an example that does not involve any
46 computation but uses new syntax.
47 </p>
48 <ul>
49 <li>Fields that happen to be valid identifiers have no quotes</li>
50 <li><i>Trailing</i> commas at the end of arrays / objects</li>
51 <li>Comments</li>
52 <li>
53 String literals use <code>"</code> or <code>'</code>. The single quote is easier on the
54 eyes but either can be used to avoid escaping the other, e.g. <code>"Farmer's Gin"</code>
55 instead of <code>'Farmer\'s Gin'</code>.
56 </li>
57 <li>
58 Text blocks <code>|||</code> allow verbatim text across multiple lines.
59 </li>
60 <li>
61 Verbatim strings <code>@'foo'</code> and <code>@"foo"</code> are for single lines.
62 </li>
63 <li>
64 Large numeric literals may be rendered more readable by using underscores, e.g. <code>1_000_000</code>.
65 </li>
66 </ul>
67 <p>
68 Using the interactive demo below, try modifying the strings / quantities. Try adding a "Dry
69 Manhattan" which uses dry red vermouth and is garnished with a lemon slice.
70 </p>
71 </div>
72 <div style="clear: both"></div>
73 </div>
74 </div>
75
76 {% include demo.inc name="syntax" %}
77
78 <div class="hgroup">
79 <div class="hgroup-inline">
80 <div class="panel">
81 <h2 id="variables">Variables</h2>
82 </div>
83 <div style="clear: both"></div>
84 </div>
85 </div>
86
87 <div class="hgroup">
88 <div class="hgroup-inline">
89 <div class="panel">
90 <p>
91 Variables are the simplest way to avoid duplication.
92 </p>
93 <ul>
94 <li>The <code>local</code> keyword defines a variable.</li>
95 <li>Variables defined next to fields end with a comma (<code>,</code>).</li>
96 <li>All other cases end with a semicolon (<code>;</code>).</li>
97 </ul>
98 <p>
99 Try factoring out <code>"Simple Syrup"</code> to the top level or to the same level as
100 the <code>pour</code> variable.
101 </p>
102 </div>
103 <div style="clear: both"></div>
104 </div>
105 </div>
106
107 {% include demo.inc name="variables" %}
108
109 <div class="hgroup">
110 <div class="hgroup-inline">
111 <div class="panel">
112 <h2 id="references">References</h2>
113 </div>
114 <div style="clear: both"></div>
115 </div>
116 </div>
117
118 <div class="hgroup">
119 <div class="hgroup-inline">
120 <div class="panel">
121 <p>
122 Another way to avoid duplication is to refer to another part of the structure.
123 </p>
124 <ul>
125 <li><code>self</code> refers to the current object.</li>
126 <li><code>$</code> refers to the outer-most object.</li>
127 <li><code>['foo']</code> looks up a field.</li>
128 <li><code>.foo</code> can be used if the field name is an identifier.</li>
129 <li><code>[10]</code> looks up an array element.</li>
130 <li>Arbitrarily long paths are allowed.</li>
131 <li>Array slices like <code>arr[10:20:2]</code> are allowed, like in Python.</li>
132 <li>Strings can be looked up / sliced too, by unicode codepoint.</li>
133 </ul>
134 <p>
135 Another name for a Tom Collins is a Gin Fizz. Try adding an alias for that.
136 </p>
137 </div>
138 <div style="clear: both"></div>
139 </div>
140 </div>
141
142 {% include demo.inc name="references" %}
143
144 <div class="hgroup">
145 <div class="hgroup-inline">
146 <div class="panel">
147 <p>
148 In order to refer to objects between the current and outer-most object, we use a variable to
149 create a name for that level:
150 </p>
151 </div>
152 <div style="clear: both"></div>
153 </div>
154 </div>
155
156 {% include demo.inc name="inner-reference" %}
157
158 <div class="hgroup">
159 <div class="hgroup-inline">
160 <div class="panel">
161 <h2 id="arithmetic">Arithmetic</h2>
162 </div>
163 <div style="clear: both"></div>
164 </div>
165 </div>
166
167 <div class="hgroup">
168 <div class="hgroup-inline">
169 <div class="panel">
170 <p>
171 Arithmetic includes numeric operations like multiplication but also various operations on
172 other types.
173 </p>
174 <ul>
175 <li>Use floating point arithmetic, bitwise ops, boolean logic.</li>
176 <li>
177 Strings may be concatenated with <code>+</code>, which implicitly converts
178 one operand to string if needed.
179 </li>
180 <li>Two strings can be compared with <code>&lt;</code> (unicode codepoint order).</li>
181 <li>
182 Objects may be combined with <code>+</code> where the right-hand side wins field
183 conflicts.
184 </li>
185 <li>Test if a field is in an object with <code>in</code>.</li>
186 <li><code>==</code> is deep value equality.</li>
187 <li>Python-compatible string formatting is available via <code>%</code>. When combined with
188 <code>|||</code> this can be used for templating text files.</li>
189 </ul>
190 <p>Try dividing by zero to see what happens.</p>
191 </div>
192 <div style="clear: both"></div>
193 </div>
194 </div>
195
196 {% include demo.inc name="arith" %}
197
198 <div class="hgroup">
199 <div class="hgroup-inline">
200 <div class="panel">
201 <h2 id="functions">Functions</h2>
202 </div>
203 <div style="clear: both"></div>
204 </div>
205 </div>
206
207 <div class="hgroup">
208 <div class="hgroup-inline">
209 <div class="panel">
210 <p>
211 Like Python, functions have positional parameters, named parameters, and default arguments.
212 Closures are also supported. The examples below should demonstrate the syntax. Many
213 functions are already defined in the <a href=/ref/stdlib.html>standard library</a>.
214 </p>
215 <p>
216 Try writing a function <code>is_even</code> that uses the modulo operator <code>%</code> and
217 returns either true or false.
218 </p>
219 </div>
220 <div style="clear: both"></div>
221 </div>
222 </div>
223
224 {% include demo.inc name="functions" %}
225
226 <div class="hgroup">
227 <div class="hgroup-inline">
228 <div class="panel">
229 <p>
230 And here's an example with cocktails.
231 </p>
232 </div>
233 <div style="clear: both"></div>
234 </div>
235 </div>
236
237 {% include demo.inc name="sours" %}
238
239 <div class="hgroup">
240 <div class="hgroup-inline">
241 <div class="panel">
242 <h2 id="conditionals">Conditionals</h2>
243 </div>
244 <div style="clear: both"></div>
245 </div>
246 </div>
247
248 <div class="hgroup">
249 <div class="hgroup-inline">
250 <div class="panel">
251 <p>
252 Conditional expressions look like <code>if b then e else e</code>. The
253 <code>else</code> branch is optional and defaults to <code>null</code>.
254 </p>
255 <p>
256 Try adding the missing Large Virgin Mojito:
257 </p>
258 </div>
259 <div style="clear: both"></div>
260 </div>
261 </div>
262
263 {% include demo.inc name="conditionals" %}
264
265 <div class="hgroup">
266 <div class="hgroup-inline">
267 <div class="panel">
268 <h2 id="computed_field_names">Computed Field Names</h2>
269 </div>
270 <div style="clear: both"></div>
271 </div>
272 </div>
273
274 <div class="hgroup">
275 <div class="hgroup-inline">
276 <div class="panel">
277 <p>
278 Jsonnet objects can be used like a <code>std::map</code> or similar datastructures from
279 regular languages.
280 </p>
281 <ul>
282 <li>Recall that a field lookup can be computed with <code>obj[e]</code></li>
283 <li>The definition equivalent is <code>{[e]: </code>... <code>}</code></li>
284 <li>
285 <code>self</code> or object locals cannot be accessed when field names are being computed,
286 since the object is not yet constructed.
287 </li>
288 <li>
289 If a field name evaluates to <code>null</code> during object construction, the field is
290 omitted. This works nicely with the default false branch of a conditional (see below).
291 </li>
292 </ul>
293 <p>
294 Try adding <code>[self.f]: 'f'</code> to see what happens, while pondering what that
295 could even mean!
296 </p>
297 </div>
298 <div style="clear: both"></div>
299 </div>
300 </div>
301
302 {% include demo.inc name="computed-fields" %}
303
304 <div class="hgroup">
305 <div class="hgroup-inline">
306 <div class="panel">
307 <h2 id="comprehension">Array and Object Comprehension</h2>
308 </div>
309 <div style="clear: both"></div>
310 </div>
311 </div>
312
313 <div class="hgroup">
314 <div class="hgroup-inline">
315 <div class="panel">
316 <p>
317 What if you want to make an array or object and you don't know how many elements / fields
318 they will have at run-time? Jsonnet has Python-style array and object comprehension
319 constructs to allow this.
320 </p>
321 <ul>
322 <li>Any nesting of <code>for</code> and <code>if</code> can be used.</li>
323 <li>The nest behaves like a loop nest, although the body is written first.</li>
324 </ul>
325 </div>
326 <div style="clear: both"></div>
327 </div>
328 </div>
329
330 {% include demo.inc name="comprehensions" %}
331
332 <div class="hgroup">
333 <div class="hgroup-inline">
334 <div class="panel">
335 <p>
336 Below, a less contrived example. The first cocktail has equal parts of three ingredients.
337 The second iterates over an array of records - name prefix and fruit juice.
338 </p>
339 <p>
340 Try adding a "Red Screwdriver" with cranberry juice.
341 </p>
342 </div>
343 <div style="clear: both"></div>
344 </div>
345 </div>
346
347 {% include demo.inc name="cocktail-comprehensions" %}
348
349 <div class="hgroup">
350 <div class="hgroup-inline">
351 <div class="panel">
352 <h2 id="imports">Imports</h2>
353 </div>
354 <div style="clear: both"></div>
355 </div>
356 </div>
357
358 <div class="hgroup">
359 <div class="hgroup-inline">
360 <div class="panel">
361 <p>
362 It's possible to import both code and raw data from other files.
363 </p>
364 <ul>
365 <li>The <code>import</code> construct is like copy/pasting Jsonnet code.</li>
366 <li>Files designed for import by convention end with <tt>.libsonnet</tt></li>
367 <li>Raw JSON can be imported this way too.</li>
368 <li>The <code>importstr</code> construct is for verbatim UTF-8 text.</li>
369 <li>The <code>importbin</code> construct is for verbatim binary data.</li>
370 </ul>
371 <p>
372 Usually, imported Jsonnet content is stashed in a top-level local variable. This
373 resembles the way other programming languages handle modules. Jsonnet libraries typically
374 return an object, so that they can easily be extended. Neither of these conventions are
375 enforced.
376 </p>
377 <p>
378 Try including the Cosmopolitan from the library.
379 </p>
380 </div>
381 <div style="clear: both"></div>
382 </div>
383 </div>
384
385 <div class="inverse hgroup">
386 <div class=hgroup-inline>
387 <div class="tab-window-input" id="imports-input">
388 <div class="tab-header">
389 </div>
390 <textarea id=imports-jsonnet>
391 {% include examples/imports.jsonnet %}
392 </textarea>
393 <textarea id=martinis-libsonnet>
394 {% include examples/martinis.libsonnet %}
395 </textarea>
396 <textarea id=garnish-txt>
397 {% include examples/garnish.txt %}
398 </textarea>
399 </div>
400 <div class="bigarrow">➡</div>
401 <div class="tab-window-output" id="imports-output">
402 <div class="tab-header">
403 <div class=selected onclick="tab_output_click(this, 'imports-json-output')">output.json</div>
404 </div>
405 <textarea readonly class="selected code-json" id="imports-json-output">
406 {% include examples/imports.jsonnet.golden %}
407 </textarea>
408 </div>
409 <script>
410 demo(
411 'imports-input',
412 {
413 'imports-jsonnet': 'imports.jsonnet',
414 'martinis-libsonnet': 'martinis.libsonnet',
415 'garnish-txt': 'garnish.txt',
416 },
417 'imports.jsonnet',
418 'imports-output',
419 false,
420 false
421 );
422 </script>
423 <div style="clear: both"></div>
424 </div>
425 </div>
426
427 <div class="hgroup">
428 <div class="hgroup-inline">
429 <div class="panel">
430 <p>
431 The following example shows how to write libraries of functions. It also shows how to
432 define a local variable in the scope of a function. Try adding the Bee's Knees from above
433 using the utility library.
434 </p>
435 </div>
436 <div style="clear: both"></div>
437 </div>
438 </div>
439
440 <div class="inverse hgroup">
441 <div class=hgroup-inline>
442 <div class="tab-window-input" id="negroni-input">
443 <div class="tab-header">
444 </div>
445 <textarea id=negroni-jsonnet>
446 {% include examples/negroni.jsonnet %}
447 </textarea>
448 <textarea id=utils-libsonnet>
449 {% include examples/utils.libsonnet %}
450 </textarea>
451 </div>
452 <div class="bigarrow">➡</div>
453 <div class="tab-window-output" id="negroni-output">
454 <div class="tab-header">
455 <div class=selected onclick="tab_output_click(this, 'negroni-json-output')">output.json</div>
456 </div>
457 <textarea readonly class="selected code-json" id="negroni-json-output">
458 {% include examples/negroni.jsonnet.golden %}
459 </textarea>
460 </div>
461 <script>
462 demo(
463 'negroni-input',
464 {
465 'negroni-jsonnet': 'negroni.jsonnet',
466 'utils-libsonnet': 'utils.libsonnet',
467 },
468 'negroni.jsonnet',
469 'negroni-output',
470 false,
471 false
472 );
473 </script>
474 <div style="clear: both"></div>
475 </div>
476 </div>
477
478 <div class="hgroup">
479 <div class="hgroup-inline">
480 <div class="panel">
481 <h2 id="errors">Errors</h2>
482 </div>
483 <div style="clear: both"></div>
484 </div>
485 </div>
486
487 <div class="hgroup">
488 <div class="hgroup-inline">
489 <div class="panel">
490 <p>
491 Errors can arise from the language itself (e.g. an array overrun) or thrown from Jsonnet
492 code. Stack traces provide context for the error.
493 </p>
494 <ul>
495 <li>To raise an error: <code>error "foo"</code></li>
496 <li>To assert a condition before an expression: <code>assert "foo";</code></li>
497 <li>A custom failure message: <code>assert "foo" : "message";</code></li>
498 <li>Assert fields have a property: <code>assert self.f == 10,</code></li>
499 <li>With custom failure message: <code>assert self.f == 10 : "message",</code></li>
500 </ul>
501 <p>
502 Try modifying the code below to trigger the assertion failures, and observe the error
503 messages and stack traces that result.
504 </p>
505 <p>
506 You might wonder why the divide-by-zero is not triggered in the equal_parts function, since
507 that code occurs before the <code>std.length()</code> check. Jsonnet is a lazy
508 language, so variable initializers are not evaluated until the variable is used. Evaluation
509 order is very hard to notice. It only becomes relevant when code throws errors, or takes a
510 long time to execute. For more discussion of laziness, see <a
511 href=/articles/design.html>design rationale</a>.
512 </p>
513 </div>
514 <div style="clear: both"></div>
515 </div>
516 </div>
517
518 {% include demo.inc name="error-examples" %}
519
520 <div class="hgroup">
521 <div class="hgroup-inline">
522 <div class="panel">
523 <h2 id="parameterize-entire-config">Parameterize Entire Config</h2>
524 </div>
525 <div style="clear: both"></div>
526 </div>
527 </div>
528
529 <div class="hgroup">
530 <div class="hgroup-inline">
531 <div class="panel">
532 <p>
533 Jsonnet is hermetic: It always generates the same data no matter the execution environment.
534 This is an important property but there are times when you want a few carefully chosen
535 parameters at the top level. There are two ways to do this, with different properties.
536 </p>
537 <ul>
538 <li>
539 <b>External variables</b>, which are accessible anywhere in the config, or any file,
540 using <code>std.extVar("foo")</code>.
541 </li>
542 <li>
543 <b>Top-level arguments</b>, where the whole config is expressed as a function.
544 </li>
545 </ul>
546 <p>
547 To illustrate the differences, We'll show how the same example is expressed in each case.
548 </p>
549 </div>
550 <div style="clear: both"></div>
551 </div>
552 </div>
553
554 <div class="hgroup">
555 <div class="hgroup-inline">
556 <div class="panel">
557 <h3 id="external-variables">External variables</h3>
558 </div>
559 <div style="clear: both"></div>
560 </div>
561 </div>
562
563 <div class="hgroup">
564 <div class="hgroup-inline">
565 <div class="panel">
566 <p>
567 The following example binds two external variables, listed below. Any Jsonnet value can be
568 bound to an external variable, even functions.
569 </p>
570 <ul>
571 <li><code>prefix</code> is bound to the string <code>"Happy Hour "</code></li>
572 <li><code>brunch</code> is bound to <code>true</code></li>
573 </ul>
574 <p>
575 The values are configured when the Jsonnet virtual machine is initialized, by passing either
576 1) Jsonnet code (which evaluates to the value), 2) or a raw string. The latter is just a
577 convenience, because escaping a string to pass it as Jsonnet code can be tedious. To make
578 this concrete, the above variables can be configured with the following commandline:
579 </p>
580 <pre>jsonnet --ext-str prefix="Happy Hour " \
581 --ext-code brunch=true ...
582 </pre>
583 </div>
584 <div style="clear: both"></div>
585 </div>
586 </div>
587
588 <div class="inverse hgroup">
589 <div class=hgroup-inline>
590 <div class="tab-window-input" id="top-level-ext-input">
591 <div class="tab-header">
592 </div>
593 <textarea id=top-level-ext-jsonnet>
594 {% include examples/top-level-ext.jsonnet %}
595 </textarea>
596 <textarea id=library-ext-libsonnet>
597 {% include examples/library-ext.libsonnet %}
598 </textarea>
599 </div>
600 <div class="bigarrow">➡</div>
601 <div class="tab-window-output" id="top-level-ext-output">
602 <div class="tab-header">
603 <div class=selected onclick="tab_click(this, 'top-level-ext-json-output')">output.json</div>
604 </div>
605 <textarea readonly class="selected code-json" id="top-level-ext-json-output">
606 {% include examples/top-level-ext.jsonnet.golden %}
607 </textarea>
608 </div>
609 <script>
610 demo(
611 'top-level-ext-input',
612 {
613 'top-level-ext-jsonnet': 'top-level-ext.jsonnet',
614 'library-ext-libsonnet': 'library-ext.libsonnet',
615 },
616 'top-level-ext.jsonnet',
617 'top-level-ext-output',
618 false,
619 false,
620 {
621 ext_str: {
622 prefix: 'Happy Hour ',
623 },
624 ext_code: {
625 brunch: 'true',
626 },
627 }
628 );
629 </script>
630 <div style="clear: both"></div>
631 </div>
632 </div>
633
634 <div class="hgroup">
635 <div class="hgroup-inline">
636 <div class="panel">
637 <h3 id="top-level-arguments">Top-level arguments</h3>
638 </div>
639 <div style="clear: both"></div>
640 </div>
641 </div>
642
643 <div class="hgroup">
644 <div class="hgroup-inline">
645 <div class="panel">
646 <p>
647 Alternatively the same code can be written using top-level arguments, where the whole config
648 is written as a function. How does this differ to using external variables?
649 </p>
650 <ul>
651 <li>Values must be explicitly threaded through files</li>
652 <li>Default values can be provided</li>
653 <li>A config with top-level arguments can also be imported as a library and called as a
654 function with parameters passed in</li>
655 </ul>
656 <p>
657 Generally, top-level arguments are the safer and easier way to parameterize an entire
658 config, because the variables are not global and it's clear what parts of the config
659 are dependent on their environment. However, they do require more explicit threading of
660 the values into other imported code. Here's the equivalent invocation of the Jsonnet
661 command-line tool:
662 </p>
663 <pre>jsonnet --tla-str prefix="Happy Hour " \
664 --tla-code brunch=true ...
665 </pre>
666 </div>
667 <div style="clear: both"></div>
668 </div>
669 </div>
670
671 <div class="inverse hgroup">
672 <div class=hgroup-inline>
673 <div class="tab-window-input" id="top-level-tla-input">
674 <div class="tab-header">
675 </div>
676 <textarea id=top-level-tla-jsonnet>
677 {% include examples/top-level-tla.jsonnet %}
678 </textarea>
679 <textarea id=library-tla-libsonnet>
680 {% include examples/library-tla.libsonnet %}
681 </textarea>
682 </div>
683 <div class="bigarrow">➡</div>
684 <div class="tab-window-output" id="top-level-tla-output">
685 <div class="tab-header">
686 <div class=selected onclick="tab_click(this, 'top-level-tla-json-output')">output.json</div>
687 </div>
688 <textarea readonly class="selected code-json" id="top-level-tla-json-output">
689 {% include examples/top-level-tla.jsonnet.golden %}
690 </textarea>
691 </div>
692 <script>
693 demo(
694 'top-level-tla-input',
695 {
696 'top-level-tla-jsonnet': 'top-level-tla.jsonnet',
697 'library-tla-libsonnet': 'library-tla.libsonnet',
698 },
699 'top-level-tla.jsonnet',
700 'top-level-tla-output',
701 false,
702 false,
703 {
704 tla_str: {
705 prefix: 'Happy Hour ',
706 },
707 tla_code: {
708 brunch: 'true',
709 },
710 }
711 );
712 </script>
713 <div style="clear: both"></div>
714 </div>
715 </div>
716
717 <div class="hgroup">
718 <div class="hgroup-inline">
719 <div class="panel">
720 <h2 id="object-orientation">Object-Orientation</h2>
721 </div>
722 <div style="clear: both"></div>
723 </div>
724 </div>
725
726 <div class="hgroup">
727 <div class="hgroup-inline">
728 <div class="panel">
729 <p>
730 In general, object-orientation makes it easy to define many variants from a single "base".
731 Unlike Java, C++ and Python, where classes extend other classes, in Jsonnet, objects extend
732 other objects. We have already discussed some of the raw ingredients for this, albeit in
733 isolation:
734 </p>
735 <ul>
736 <li>Objects (which we inherit from JSON)</li>
737 <li>The <dfn>object composition</dfn> operator <code>+</code>, which merges two objects,
738 choosing the right hand side when fields collide</li>
739 <li>The <code>self</code> keyword, a reference to the current object</li>
740 </ul>
741 <p>
742 When these features are combined together and with the following <i>new</i> features, things
743 get a lot more interesting:
744 </p>
745 <ul>
746 <li>Hidden fields, defined with <code>::</code>, which do not appear in generated JSON</li>
747 <li>The <code>super</code> keyword is used to access fields on a base object.</li>
748 <li>The <code>+:</code> field syntax for overriding deeply nested fields</li>
749 </ul>
750 </div>
751 <div style="clear: both"></div>
752 </div>
753 </div>
754
755 <div class="hgroup">
756 <div class="hgroup-inline">
757 <div class="panel">
758 <p>
759 The following example is contrived but demonstrates a few of the features using the
760 classical language of object-orientation, "derived" and "base".
761 </p>
762 </div>
763 <div style="clear: both"></div>
764 </div>
765 </div>
766
767 {% include demo.inc name="oo-contrived" %}
768
769 <div class="hgroup">
770 <div class="hgroup-inline">
771 <div class="panel">
772 <p>
773 Let's make it more concrete by mixing some cocktails that are quite similar from a
774 template that draws out their similarities. In these examples we use implicit object
775 composition: In the common case where you write <code>foo + {</code> ... <code>}</code>,
776 that is, you have a <code>+</code> operation with a variable on the left and an object
777 definition (something starting with <code>{</code>) on the right, then the <code>+</code>
778 can be elided. This also works if the left hand side is a field access (e.g.,
779 <code>a.b</code>, or <code>a[b]</code>), function call (e.g., <code>f(a)</code>),
780 or parenthesised expression.
781 </p>
782 <p>
783 Try explicitly adding the + in the 4 cases, below.
784 </p>
785 </div>
786 <div style="clear: both"></div>
787 </div>
788 </div>
789
790 <div class="inverse hgroup">
791 <div class=hgroup-inline>
792 <div class="tab-window-input" id="sours-oo-input">
793 <div class="tab-header">
794 </div>
795 <textarea id=sours-oo-jsonnet>
796 {% include examples/sours-oo.jsonnet %}
797 </textarea>
798 <textarea id=templates-libsonnet>
799 {% include examples/templates.libsonnet %}
800 </textarea>
801 </div>
802 <div class="bigarrow">➡</div>
803 <div class="tab-window-output" id="sours-oo-output">
804 <div class="tab-header">
805 <div class=selected onclick="tab_output_click(this, 'sours-oo-json-output')">output.json</div>
806 </div>
807 <textarea readonly class="selected code-json" id="sours-oo-json-output">
808 {% include examples/sours-oo.jsonnet.golden %}
809 </textarea>
810 </div>
811 <script>
812 demo(
813 'sours-oo-input',
814 {
815 'sours-oo-jsonnet': 'sours-oo.jsonnet',
816 'templates-libsonnet': 'templates.libsonnet',
817 },
818 'sours-oo.jsonnet',
819 'sours-oo-output',
820 false,
821 false
822 );
823 </script>
824 <div style="clear: both"></div>
825 </div>
826 </div>
827
828 <div class="hgroup">
829 <div class="hgroup-inline">
830 <div class="panel">
831 <p>
832 The key to making Jsonnet object-oriented is that the <code>self</code> keyword is "late
833 bound". In other words, <code>self.foo</code> can have its meaning altered by overriding it
834 on the right hand side of a <code>+</code>. This would not be the case in a simple "object
835 merge" semantics of <code>+</code>, where <code>self.foo</code> would be fully evaluated to
836 a concrete value before we did the <code>+</code>. In the above example, the left hand side
837 of the <code>+</code> is <code>templates.Sour</code> (the <code>+</code> is implicit) and
838 the right hand side is an object that overrides <code>spirit</code>. So in the
839 <code>templates.Sour</code> definition (in <tt>templates.libsonnet</tt>) that causes the
840 reference to <code>drink.spirit</code> in the ingredients list to return
841 <code>"Whiskey"</code> rather than raise the error <code>'Must override "spirit"'</code>.
842 </p>
843 <p>
844 Most of the abstraction in the above example could have been achieved with functions instead
845 of object-orientation. Indeed, last time we spoke about sours, we used functions for that
846 very purpose. In general, the choice of whether to use functions or object-orientation is
847 quite a complex one, and can be driven as much by instinct as by anything else. The most
848 obvious difference is that with object-orientation, you can override any field from the
849 resulting object, whereas with functions you can only pass arguments for the specific
850 parameters of the function. So, e.g., if the <code>templates.Sour</code> had been
851 implemented as a function that did not parameterize the ingredients list in general way,
852 then it would not have been possible to add that extra ingredient to form the Nor'Easter.
853 </p>
854 <p>
855 So far, every example has used a literal object on the right hand side of the
856 <code>+</code>. This is consistent with the majority of object-oriented languages. But
857 Jsonnet is more general in that is has <a
858 href="https://www.bracha.org/oopsla90.pdf">mixins</a>. A Mixin allows you to take some
859 "overrides" and instead of just applying them to an existing object to get a new object, you
860 can pass them around as a first class value and apply them to any object you want. The
861 following example shows mixins being used to modify cocktails in a general way:
862 </p>
863 </div>
864 <div style="clear: both"></div>
865 </div>
866 </div>
867
868 <div class="inverse hgroup">
869 <div class=hgroup-inline>
870 <div class="tab-window-input" id="mixins-input">
871 <div class="tab-header">
872 </div>
873 <textarea id=mixins-jsonnet>
874 {% include examples/mixins.jsonnet %}
875 </textarea>
876 <textarea id=sours-oo-jsonnet2>
877 {% include examples/sours-oo.jsonnet %}
878 </textarea>
879 <textarea id=templates-libsonnet2>
880 {% include examples/templates.libsonnet %}
881 </textarea>
882 </div>
883 <div class="bigarrow">➡</div>
884 <div class="tab-window-output" id="mixins-output">
885 <div class="tab-header">
886 <div class=selected onclick="tab_output_click(this, 'mixins-json-output')">output.json</div>
887 </div>
888 <textarea readonly class="selected code-json" id="mixins-json-output">
889 {% include examples/mixins.jsonnet.golden %}
890 </textarea>
891 </div>
892 <script>
893 demo(
894 'mixins-input',
895 {
896 'mixins-jsonnet': 'mixins.jsonnet',
897 'sours-oo-jsonnet2': 'sours-oo.jsonnet',
898 'templates-libsonnet2': 'templates.libsonnet',
899 },
900 'mixins.jsonnet',
901 'mixins-output',
902 false,
903 false
904 );
905 </script>
906 <div style="clear: both"></div>
907 </div>
908 </div>
909
910 <div class="hgroup">
911 <div class="hgroup-inline">
912 <div class="panel">
913 <p>
914 Thank you for completing the Jsonnet tutorial! We hope you enjoyed the accompanying
915 beverages. At this point, you should be able to build concoctions of your own. If you want
916 to read about the Jsonnet language in greater and more systematic detail, we recommend the
917 language <a href="/ref/language.html">reference</a>.
918 </p>
919 </div>
920 <div style="clear: both"></div>
921 </div>
922 </div>
923
924 <div class="hgroup">
925 <div class="hgroup-inline">
926 <div class="panel">
927 </div>
928 <div style="clear: both"></div>
929 </div>
930 </div>