4 ms·
I think Singletons are ok when the core language's runtime promises to take care of them. They are also ok for things like the abstract state objects in a stat
by bayareaguy 19y ago
I think Singletons are ok when the core language's runtime promises to take care of them. They are also ok for things like the abstract state objects in a state machine and for special immutable Sentinal objects (e.g. Lisp nil, Python None, ...).
- sah 19y agoGood point: things like states, true, false, and nil make sense. I think the commonality there is that those are all immutable values whose only purpose is to have a fast object identity comparison. In a way, they're similar to global variables which are all members of the same class, except that it's nice for them to have separate types.
- sah 19y agoIn fact, you could think of Python's None as being a singleton, but True and False being the two allowed instances of a, uh, doubleton? And similarly, states might be implemented as a limited set of allowed values of a single class. This reminds me of Haskell's algebraic types. This maybe isn't quite "Singleton", but it is at least a useful case for limiting the number of allowed values of a class.