Chapter 5
Types and Classes
Singletons
Singleton types are used to indicate individual objects. When determining whether a
singleton specializer matches a given object, the object must be ==
to the
object used to create the singleton.
A singleton for an object is created by passing the object to the
function singleton
, or by calling the function make
on the
class <singleton>
.
Singleton methods are considered more specific than methods defined on an object's class. Singletons are the most specific specializer.
define method double (thing :: singleton(#"cup"))
#"pint"
end method
double (#"cup")
⇒
#"pint"
Dylan provides a concise syntax for singletons used as method specializers. The following
definition is equivalent to the previous one; it generates a call to the binding
of singleton
in the current lexical environment.
define method double (thing == #"cup")
#"pint"
end method
double (#"cup")
⇒
#"pint"