1 /** This file is part of Shapes.
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
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.
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/>.
16 ** Copyright 2015 Henrik Tidefelt
19 /** This example demonstrates private namespaces.
22 /** Pushing a named namespace sets up a new unique private namespace: **/
25 /** Open the private namespace a first time: **/
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:
31 Shapes..IO..•stdout << `Current namespace: ´ << (resolved_identifier_string @@) << "{n}
34 /** Access implementation detail from the private namespace using relative identifier
35 ** with empty namespace path:
37 Shapes..IO..•stdout << (resolved_identifier_string x) << "{n}
39 /** Open the private namespace a second time: **/
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. **/
50 /** Expose the functions foo and bar from the private namespace by
51 ** providing bindings in the normal namespace:
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}
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: **/
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}
72 /** Neither is x in scope here. **/
75 /** The exposed bindings can still be accessed in the normal namespace: **/
76 Shapes..IO..•stdout << (resolved_identifier_string A..baz) << "{n}