Fix Makefile whitespace errors
[shapes.git] / examples / doc / private-namespace.blank
blob5f0d16e8050856d767b2b37d566de8a90a840ed4
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 2015 Henrik Tidefelt
17  **/
19 /** This example demonstrates private namespaces.
20  **/
22 /** Pushing a named namespace sets up a new unique private namespace: **/
23 ##push A
25 /** Open the private namespace a first time: **/
26 ##push -
27 x: 1 /** Implementation detail of A. **/
28 /** Source code reflection of the private namespace shows a namespace identifier in
29  ** the form of a unique number:
30  **/
31 Shapes..IO..•stdout << `Current namespace: ´ << (resolved_identifier_string @@) << "{n}
32 ##pop -
34 /** Access implementation detail from the private namespace using relative identifier
35  ** with empty namespace path:
36  **/
37 Shapes..IO..•stdout << (resolved_identifier_string x) << "{n}
39 /** Open the private namespace a second time: **/
40 ##push -
41 Shapes..IO..•stdout << `Current namespace: ´ << (resolved_identifier_string @@) << "{n}
42 /** The private variable x is still in scope here. **/
43 Shapes..IO..•stdout << (resolved_identifier_string x) << "{n}
44 /** Implement some functions in the private namespace. **/
45 foo: \ y → x + y
46 bar: \ y → x - y
47 baz: \ y → x * y
48 ##pop -
50 /** Expose the functions foo and bar from the private namespace by
51  ** providing bindings in the normal namespace:
52  **/
53 ": foo /** Refers to binding in private namespace; this is not a circular refrence. **/
54 b: bar /** Expose bar under a different name. **/
55 baz: - /** Special syntax for exposing private namespace variable of the same name. **/
57 /** Verify that the private namespace has precedence over the normal namespace: **/
58 Shapes..IO..•stdout << (resolved_identifier_string foo) << "{n}
60 ##pop A
62 /** One cannot access the private binding here; something like A..x won't work. **/
64 /** Pushing the same named namespace again sets up a new unique private namespace: **/
65 ##push A
66 ##push -
67 Shapes..IO..•stdout << `Current namespace: ´ << (resolved_identifier_string @@) << "{n}
68 /** The variable x is not in scope here, since this is a different private namespace. **/
69 /** The foo in the other private namespace is no longer in scope: **/
70 Shapes..IO..•stdout << (resolved_identifier_string foo) << "{n}
71 ##pop -
72 /** Neither is x in scope here. **/
73 ##pop A
75 /** The exposed bindings can still be accessed in the normal namespace: **/
76 Shapes..IO..•stdout << (resolved_identifier_string A..baz) << "{n}