www.delorie.com/gnu/docs/smalltalk/gst_14.html   search  
 
Buy GNU books!


GNU Smalltalk User's Guide

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.2.5 Using namespaces

Using namespaces if often merely a matter of rewriting the loading script this way:
 
    Smalltalk addSubspace: #NewNS!
    Namespace current: NewNS!
    ...
    Namespace current: Smalltalk!

Also remember that pool dictionaries are actually "pool namespaces", in the sense that including a namespace in the pool dictionaries list will automatically include its superspaces too. Declaring a namespace as a pool dictionaries is similar in this way to C++'s using namespace declaration.

Finally, be careful when working with fundamental system classes. Although you can use code like
 
    Smalltalk Set variableSubclass: #Set
        ...
        category: 'My application-Extensions'

or the equivalent syntax Set extend, this approach won't work when applied to core classes. For example, you might be successful with a Set or WriteStream object, but subclassing SmallInteger this way can bite you in strange ways: integer literals will still belong to the Smalltalk dictionary's version of the class (this holds for Arrays, Strings, etc. too), primitive operations will still answer standard Smalltalk SmallIntegers, and so on. Or, variableWordSubclasses will recognize 32-bit Smalltalk LargeInteger objects, but not LargeIntegers belonging to your own namespace.

Unfortunately this problem is not easy to solve since Smalltalk has to cache the OOPs of determinate class objects for speed--it would not be feasible to lookup the environment to which sender of a message belongs every time the + message was sent to an Integer.

So, GNU Smalltalk namespaces cannot yet solve 100% of the problem of clashes between extensions to a class--for that you'll still have to rely on prefixes to method names. But they do solve the problem of clashes between class names and pool dictionary names, so you might want to give them a try. An example of using namespaces is given by the `examples/Publish.st' file in the GNU Smalltalk source code directory.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003