2.34. Namespaces

Modules are closed: everything in a module has to be defined in one place. There is an open form of modules called namespaces, illustrated below. Namespace can be built up incrementally thru the program.

Executable code in a namespace is executed after all code not in namespaces. All the code in one namespace is executed before that of another. The order in which code of a namespace is executed is the order of writing of the first use of each namespace.

The concatenated contents of a namespace is equivalent to a module.

Namespace can be polymorphic and have constraints just like modules. The text specifying the type variables and any constraints must be repeated verbatim for each use of the namespace.

Start felix section to tut/tutorial/tut-1.34-0.flx[1 /1 ]
     1: #line 3648 "./lpsrc/flx_tutorial.pak"
     2: //Check namespaces
     3: #import <flx.flxh>
     4: namespace A { var x = 1; }
     5: namespace B { var x = 1; }
     6: namespace A { var y = x+1; }
     7: namespace B { var y = x+1; }
     8: 
     9: namespace A { print y; endl; }
    10: namespace B { print y; endl; }
    11: 
    12: var A::z = y;
    13: var B::z = y;
    14: 
    15: namespace A { print A::y; endl; }
    16: namespace B { print B::y; endl; }
    17: 
End felix section to tut/tutorial/tut-1.34-0.flx[1]
Start data section to tut/tutorial/tut-1.34-0.expect[1 /1 ]
     1: 2
     2: 2
     3: 2
     4: 2
End data section to tut/tutorial/tut-1.34-0.expect[1]
A namespace qualifier may be used before the identifier in most definitions.
Start felix section to tut/tutorial/tut-1.34-1.flx[1 /1 ]
     1: #line 3675 "./lpsrc/flx_tutorial.pak"
     2: //Check namespaces
     3: #import <flx.flxh>
     4: 
     5: print "b4 fred"; endl;
     6: 
     7: namespace fred {
     8:   print "fred1 start"; endl;
     9:   var x = 1;
    10:   proc f() { print "f"; endl; }
    11:   print "fred1 end"; endl;
    12: }
    13: 
    14: print "between freds <not!>"; endl;
    15: 
    16: namespace fred {
    17:   print "fred2 start"; endl;
    18:   var y = 1;
    19:   proc g() { print "g"; endl; }
    20:   print "fred2 end"; endl;
    21: };
    22: 
    23: proc fred::h() { print "h"; endl; }
    24: print "after freds"; endl;
    25: 
    26: open fred;
    27: 
    28: print x; print " "; print y; endl;
    29: 
    30: f(); g(); h();
    31: 
End felix section to tut/tutorial/tut-1.34-1.flx[1]
Start data section to tut/tutorial/tut-1.34-1.expect[1 /1 ]
     1: b4 fred
     2: between freds <not!>
     3: after freds
     4: 0 0
     5: f
     6: g
     7: h
     8: fred1 start
     9: fred1 end
    10: fred2 start
    11: fred2 end
End data section to tut/tutorial/tut-1.34-1.expect[1]