[Prev][Next][Index][Thread]
Re: simple method dispatch question
in article 3A0DBA00.E287C76E@dial.pipex.com, jt at gkt37@dial.pipex.com
wrote on 2000.11.11 1:28 PM:
>
> Barry Margolin wrote:
>
>> One of the other posters said that when he said "singleton" he wasn't
>> talking about the Design Pattern with this name, and he thought it was
>> unfortunate that Dylan used the same term. But in fact, Dylan's singletons
>> *are* the Singleton Design Pattern.
This is incorrect. They are not the same. See below.
> My original reading of the DRM led me believe that they weren't
> singletons in the GOF sense because they weren't guaranteed to be
> unique, whereas the contributors to this thread clearly think otherwise.
> The DRM says:
>
> "...If a singleton for the specified object already exists,
> implementations are free to return it rather than allocate a new
> singleton."
There is a misunderstanding here about what it is that is unique. Instances
of <singleton> (which is what singleton() creates) are not guaranteed to be
unique for a given "object:" argument. But this is irrelevant. See below.
The singleton pattern and the Dylan singleton are two very different things.
The singleton pattern is used to define a class that when instantiated
always produces the same object. The point is to instantiate the class to
get an object.
Dylan's singleton() takes an object and returns a type whose only instance
is that object. The point is to provide a type for comparison against other
types. It is not a class, and (generally speaking) you do not use it as an
argument to make().
Here's an example implementation of the singleton pattern in Dylan (pardon
me if this doesn't actually work, I haven't tried it):
define class <only-one> (<object>) end;
define variable $only-one-instance = #f;
define method make( class == <only-one>, #rest rest )
$only-one-instance
| $only-one-instance := next-method()
end;
For comparison, it would perhaps be impossible or nonsensical to implement
Dylan's singleton() in C++. You could think of it as creating an RTTI value
at runtime, were this possible. Even if you could, it wouldn't provide much
utility unless you added Dylan's runtime type/object system to C++.
Getting back to the issue of uniqueness, singleton( object ) is essentially
a shorthand for make( <singleton>, object: object ). It returns an instance
of <singleton>. It does not matter whether that instance is unique for
multiple calls to make() with the same "object:" argument, because you do
not test two singleton objects with "==", generally speaking. Instead, you
test singleton objects with "=" or subtype?(). Or you test an object against
the singleton with instance?().
The "object:" argument to singleton() is not an instance of the singleton;
it is a property of it. Multiple singletons can have the same value for this
property, and they will be "=".
Yes, perhaps it is unfortunate that the singleton pattern and Dylan's
singleton have the same name, as they are very different things.
--
Chris Page
let mailto = concatenate( "page", "@", "best.com" );
References: