/** This file is part of Shapes. ** ** Shapes is free software: you can redistribute it and/or modify ** it under the terms of the GNU General Public License as published by ** the Free Software Foundation, either version 3 of the License, or ** any later version. ** ** Shapes is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** GNU General Public License for more details. ** ** You should have received a copy of the GNU General Public License ** along with Shapes. If not, see . ** ** Copyright 2008, 2014, 2015 Henrik Tidefelt **/ ##lookin ..Shapes /** ** First, let's have a look at some basic use of dynamic binding. ** If the first parameter after the name of the variable is the special function it is optimized away by the kernel. ** The second parameter after the name is the default value, and is not sent through the filter. **/ dynamic @a identity 8 f: \ b → @a + b IO..•stdout << [f 2] << "{n} IO..•stdout << @a:6 | [f 2] << "{n} IO..•stdout << [f 2] << "{n} /** ** Note that the default value is delayed, so we can require that a dynamic variable must be bound by the user. ** This is also nice to know if the default expression would be expensive to compute but rarely used. ** Another application would be to detect whether the default value is ever used. ** ** However, the filter is evaluated immediately -- this simplifies the kernel business. **/ dynamic @a_noDefault identity [error `Dynamic variable has no default binding.´] IO..•stdout << @a_noDefault:9 | @a_noDefault << ` No error, see?´ << "{n} dynamic @a_logDefault identity [Debug..log_before `The default value was used.´+"{n} ~9] IO..•stdout << @a_logDefault << ` Check out the debug log!´ << "{n} /** ** Next, we turn to dynamic expressions. **/ dynamic @c identity 10 dynamic @b identity dynamic @a + 5 @a: dynamic @c * 4 & @c: 20 | { IO..•stdout << `@b: ´ << @b << "{n} } /** ** The rest of this file illustrates how to replace part of a dynamic bindings value by using the &| operator. ** The typical application of this would be to define a set of bindings for the text state, and then define a variation ** by changing just some of the parameters. However, since this example is meant to be text-oriented we use a silly ** mix of meaningless bindings instead. **/ b0: Traits..@width:3mm & @a:20 & @b:21 & Traits..@stroking:[Traits..gray 0.5] b1: Traits..@width:1mm & @b:11 & @c:12 /** ** Note that b0 & b1 would be illegal, since they both provide bindings for Traits..@width and @b. **/ /** ** Combine b0 and b1 with priority to the bindings in b1: **/ b01: Debug..locate [] b0 &| b1 b01 | { IO..•stdout << [Debug..sourceof b01] << ` :´ << "{n} IO..•stdout << `@a: ´ << @a << "{n} IO..•stdout << `@b: ´ << @b << "{n} IO..•stdout << `@c: ´ << @c << "{n} IO..•stdout << `Traits..@width: ´ << Traits..@width << "{n} IO..•stdout << `Traits..@stroking: ´ << Traits..@stroking << "{n} } /** ** Combine b0 and b1 with priority to the bindings in b0: **/ b10: Debug..locate [] b1 &| b0 b10 | { IO..•stdout << [Debug..sourceof b10] << ` :´ << "{n} IO..•stdout << `@a: ´ << @a << "{n} IO..•stdout << `@b: ´ << @b << "{n} IO..•stdout << `@c: ´ << @c << "{n} IO..•stdout << `Traits..@width: ´ << Traits..@width << "{n} IO..•stdout << `Traits..@stroking: ´ << Traits..@stroking << "{n} }