I am new to Crystal, but a long time Ruby fan.
In Ruby, we have the Singleton module.
I tried to implement the same thing in Crystal.
I started with writing a class with the singleton property.
The class works just fine.
class Klass private SINGLETON = ::Mutex.new @@instance : self? def self.instance @@instance || SINGLETON.synchronize do @@instance ||= new end end end
However, when I tried extracting the functionality into a module, I’ll get a compilation error.
Could you show me how to do it properly? Or if this is possible at all?
One thing I like about Ruby is people always make a trade-off on type-safety with convenience and usabilities.