Namespaces: New Gem From Old Roots
Vector 16:1, page 92
Transcribed from the printed issue; not yet reviewed. Read it in the PDF of the issue, page 92
Introduction¶
The most interesting object, since nested arrays were implemented, is (in my opinion) the Namespace object. It was implemented by Dyadic Systems Ltd. in Dyalog APL Version 6. I think that developers do not use the full power of this object for several reasons. In this article I want to describe some features of namespace programming technologies that can make easy-to-program utility functions using namespaces.
Using Complex Names¶
What do I call complex names? Look at the example :
)ns Name1
#.Name1
)ns Name1.Name2
#.Name1.Name2
)ns Name3
#.Name3
)ns Name4
#.Name4
Name1.A←1
Name1.Name2.A←2
Name3.A←3
Name4.A←4
Here we create 4 namespaces and 4 variables A with different values. A simple call to these variables is described below:
Nothing interesting there. Yes?
And now try to type in the session the next example:
What value did you get? Yes, this call is equal to the call:
I call Line (A) a “Complex” call, line (B) a simple call.
Using Complex Calls¶
Using complex calls is illustrated by the examples below.
If you put all the functions to create GUI objects in the namespace #.WUTIL and try to create some of them from another namespace:
)NS #.WUTIL
#.WUTIL
)CS #.WUTIL
#.WUTIL
∇CreateForm[⎕]∇
[0] CreateForm Name
[1] Name ⎕WC'Form' 'Example'('Posn' 10 10)('Size' 50 60)
)CS #
#
Here we initialise in namespace #.WUTIL the function CreateForm that will create a form at position 10 10 with size 50 60.
and we call this function from namespace #.APP. But what happened: where was ‘Form1’ created?
Not in the current namespace. It is created in the namespace #.WUTIL.
Now change your function CreateForm as follows:
)CS #.WUTIL
#.WUTIL
∇CreateForm[⎕]∇
[0] CreateForm Name
[1] Name←(⎕IO⊃⎕NSI),'.',Name ⍝ Take old namespace name and add to Name
[2] Name ⎕WC'Form' 'Example'('Posn' 10 10)('Size' 50 60)
And now try to call it:
Line [1] of CreateForm changes the left argument from 'Form1' to '#.APP.Form1'; if we call it with a left argument of '##.APP.Form1' this line changes it to '#.APP.##.APP.Form1'. So the form is relative to the current namespace.
Now using the complex name technique we can create a form where we want to create it. Even if we call the function from the same namespace:
Conclusion¶
I think that this little paper does not complete all the possibilities of using namespaces. Let’s play with APL.