Merge branch 'ht/newline-before-EOF' into maint
[shapes.git] / examples / features / dynamic.shape
blobffc36c02b649d3732e38261d691ccd942a0d43e2
1 /** This file is part of Shapes.
2  **
3  ** Shapes is free software: you can redistribute it and/or modify
4  ** it under the terms of the GNU General Public License as published by
5  ** the Free Software Foundation, either version 3 of the License, or
6  ** any later version.
7  **
8  ** Shapes is distributed in the hope that it will be useful,
9  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
10  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  ** GNU General Public License for more details.
12  **
13  ** You should have received a copy of the GNU General Public License
14  ** along with Shapes.  If not, see <http://www.gnu.org/licenses/>.
15  **
16  ** Copyright 2008 Henrik Tidefelt
17  **/
19 /**
20  ** First, let's have a look at some basic use of dynamic binding.
21  ** If the first parameter after the name of the variable is the special <identity> function it is optimized away by the kernel.
22  ** The second parameter after the name is the default value, and is not sent through the filter.
23  **/
25 dynamic @a identity 8
26 f: \ b → @a + b
27 •stdout << [f 2] << "{n}
28 •stdout << @a:6 | [f 2] << "{n}
29 •stdout << [f 2] << "{n}
31 /**
32  ** Note that the default value is delayed, so we can require that a dynamic variable must be bound by the user.
33  ** This is also nice to know if the default expression would be expensive to compute but rarely used.
34  ** Another application would be to detect whether the default value is ever used.
35  **
36  ** However, the filter is evaluated immediately -- this simplifies the kernel business.
37  **/
39 dynamic @a_noDefault identity [error `Dynamic variable has no default binding.´]
40 •stdout << @a_noDefault:9 | @a_noDefault << ` No error, see?´ << "{n}
42 dynamic @a_logDefault identity [debuglog_before `The default value was used.´+"{n} ~9]
43 •stdout << @a_logDefault << ` Check out the debug log!´ << "{n}
46 /**
47  ** Next, we turn to dynamic expressions.
48  **/
50 dynamic @c identity 10
51 dynamic @b identity dynamic @a + 5
53   @a: dynamic @c * 4
54 & @c: 20
57   •stdout << `@b: ´ << @b << "{n}
61 /**
62  ** The rest of this file illustrates how to replace part of a dynamic bindings value by using the &| operator.
63  ** The typical application of this would be to define a set of bindings for the text state, and then define a variation
64  ** by changing just some of the parameters.  However, since this example is meant to be text-oriented we use a silly
65  ** mix of meaningless bindings instead.
66  **/
67 b0: @width:3mm & @a:20 & @b:21 & @stroking:[gray 0.5]
68 b1: @width:1mm & @b:11 & @c:12
70 /**
71  ** Note that b0 & b1 would be illegal, since they both provide bindings for @width and @b.
72  **/
74 /**
75  ** Combine b0 and b1 with priority to the bindings in b1:
76  **/
77 b01: locate [] b0 &| b1
79 b01
82   •stdout << [sourceof b01] << ` :´ << "{n}
83   •stdout << `@a: ´ << @a << "{n}
84   •stdout << `@b: ´ << @b << "{n}
85   •stdout << `@c: ´ << @c << "{n}
86   •stdout << `@width: ´ << @width << "{n}
87   •stdout << `@stroking: ´ << @stroking << "{n}
90 /**
91  ** Combine b0 and b1 with priority to the bindings in b0:
92  **/
93 b10: locate [] b1 &| b0
95 b10
98   •stdout << [sourceof b10] << ` :´ << "{n}
99   •stdout << `@a: ´ << @a << "{n}
100   •stdout << `@b: ´ << @b << "{n}
101   •stdout << `@c: ´ << @c << "{n}
102   •stdout << `@width: ´ << @width << "{n}
103   •stdout << `@stroking: ´ << @stroking << "{n}
107 /** Prevent empty output error. **/
108 [stroke (0cm,0cm)--(1cm,1cm)]