A Global Singleton is a class of which only one instance exists within a program.

Any attempt to use non-static members of the class involves performing operations on this one instance.




class Singleton(name) {
    static instance;
 
    method new(name) {
        instance := Singleton.bless(Hash(:name => name));
    }
    method new {
        Singleton.new(nil);
    }
}
 
var s1 = Singleton('foo');
say s1.name;                #=> 'foo'
say s1.object_id;           #=> '30424504'
 
var s2 = Singleton();
say s2.name;                #=> 'foo'
say s2.object_id;           #=> '30424504'
 
s2.name = 'bar';            # change name in s2
say s1.name;                #=> 'bar'