A little namespace "proposal"

goran.krampe at bluefish.se goran.krampe at bluefish.se
Tue Apr 6 15:35:58 UTC 2004


Brent Pinkney <brent.pinkney at aircom.co.za> wrote:
> Hi,
> 
> Is there a reason why the current Environments prototype is not preferred 
> anymore ?

Well, no. :) But I think it was/is a bit too complex. The nesting part
for example, I just want a flat list of Namespaces.

> I have always thought:
> 
> 	 CoolSockets Socket open.
> 
> is so much more in the flavour of object -> message = object than
> 
> 	CoolSockets::Socket open.
> 
> I like the fact that sending the class name to the namespace thing is the 
> same as sending a message to an instance of a class.
> 
> Are we sure we want to abandon this tradition and start messing with 
> syntax ?

No. :) But without having thought immensely about it - I think my little
proposal could work anyway.

The idea of a message send is nice - even though the readability is...
well. Haven't decided yet.

The idea of using a DNU + added accessor method on the fly might also be
nice. Having a particular Namespace being a subclass of Namespace (as
the class comment of Environment explains) is a nice trick - we could
create/delete them on the fly so that the user typically wouldn't need
to create them by hand.

Ok, I played a little teeny bit with this and created:

Object #()
	Namespace #()
		SqueakMap #()
		Kernel #()

I chose to be tricky here and putting it all on the class side so
instead of inheriting from IdentityDictionary I let Namespace class have
an instvar called "entries" which is a lazily initialized
IdentityDictionary. Thus I don't need to instantiate the namespace and
it is also already in the Smalltalk system dictionary :).

Then I put a DNU on the class side of Namespace like this:

doesNotUnderstand: msg

	entries ifNil: [entries _ IdentityDictionary new. ^nil].
	^entries at: msg selector

...and also #at:/#at:put: similarly.

End result is that I can get a new namespace by adding a subclass to
Namespace and then just using it directly like this:

<add subclass Grok to class Namespace>

"Lookup Humbler in namespace Grok"
Grok Humble ==> nil

"Add a binding"
Grok at: #Humble put: 12.
Grok Humble ==> 12

Also noted that 1000 lookups using DNU took 14 ms, skipping the DNU and
adding a method which does the lookup brought it down to 7 ms, and
finally just turning it into a class instvar and returning that took 1
ms.

Well, just naively playing around... 

regards, Göran



More information about the Squeak-dev mailing list