# Extracting singleton functionality into modules

**URL:** <https://forum.crystal-lang.org/t/extracting-singleton-functionality-into-modules/2162>\
**Category:** Help & Support\
**Created:** [May 24, 2020, 2:02pm UTC](https://forum.crystal-lang.org/t/extracting-singleton-functionality-into-modules/2162 "2020-05-24T14:02:20Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![midnight-wonderer](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/midnight-wonderer/32/962_2.png) [@midnight-wonderer](https://forum.crystal-lang.org/u/midnight-wonderer)\
**Post date:** [May 24, 2020, 2:02pm UTC](https://forum.crystal-lang.org/t/extracting-singleton-functionality-into-modules/2162/1 "2020-05-24T14:02:20Z")

</div>

Hi there,  
I am new to Crystal, but a long time Ruby fan.

In Ruby, we have the Singleton module.  
[https://ruby-doc.org/stdlib-2.5.1/libdoc/singleton/rdoc/Singleton.html](https://ruby-doc.org/stdlib-2.5.1/libdoc/singleton/rdoc/Singleton.html)

I tried to implement the same thing in Crystal.  
I started with writing a class with the singleton property.  
The class works just fine.

```auto
  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.

---

<div class="post-metadata">

**Author:** ![Exilor](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/exilor/32/780_2.png) [@Exilor](https://forum.crystal-lang.org/u/Exilor)\
**Post date:** [May 24, 2020, 4:05pm UTC](https://forum.crystal-lang.org/t/extracting-singleton-functionality-into-modules/2162/2 "2020-05-24T16:05:35Z")

</div>

I do it like this:

```crystal
module Singleton
  macro included
    private SINGLETON_LOCK = Mutex.new

    def self.instance : self
      @@instance || SINGLETON_LOCK.synchronize { @@instance ||= new }
    end

    macro method_added(m)
      \{% if m.name == "initialize" %}
        private def initialize
          previous_def
        end
      \{% end %}
    end
  end
end

```

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [May 25, 2020, 11:57am UTC](https://forum.crystal-lang.org/t/extracting-singleton-functionality-into-modules/2162/3 "2020-05-25T11:57:51Z")

</div>

Constants are guaranteed to be initialized only once, so instead of a class variable, you could just use a constant (being a signleton it won’t need to be reassigned anyway) and don’t need to do mutex synchronization. The runtime already takes care of that.

So the implementation of a singleton is actually quite trivial. IMO there’s no need for a generalized module to include this, when it’s just three loc:

```auto
class Foo
  INSTANCE = new

  private def initialize
  end
end

```

OFC you can still inherit this type, or call `#dup`/`#clone` to get a copy. If you want you can also restrict this. But I don’t think that’s strictly necessary. The important thing about a singleton is to not expose a public API for directly creating a new instance (`.new`).  
Another alternative to a singleton instance is simply using a module.

```auto
module FooSingleton  
  class_property foo = true
end

FooSingleton.foo

```

Btw. there are already quite a few discussions on singletons: [https://github.com/crystal-lang/crystal/pull/1399](https://github.com/crystal-lang/crystal/pull/1399), [https://github.com/crystal-lang/crystal/issues/8846](https://github.com/crystal-lang/crystal/issues/8846), [https://github.com/crystal-lang/crystal/issues/718](https://github.com/crystal-lang/crystal/issues/718)

---

<div class="post-metadata">

**Author:** ![midnight-wonderer](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/midnight-wonderer/32/962_2.png) [@midnight-wonderer](https://forum.crystal-lang.org/u/midnight-wonderer)\
**Post date:** [May 25, 2020, 2:37pm UTC](https://forum.crystal-lang.org/t/extracting-singleton-functionality-into-modules/2162/4 "2020-05-25T14:37:14Z")

</div>

Thank you  
I learned something from both answers.

However, using constant might not fit the bill in some situations.  
For example, in my case, the initialization is a binding to C object initialization; the operation of the C library is a side effect of class initialization.  
The constant approach won’t work because the side-effects would always occur.  
In contrast, singleton instance can be lazily evaluated.

Someone already made the point in the issue.  
Now the Mutex is already in the core. Do you guys still work on the singleton module?

---

<div class="post-metadata">

**Author:** ![Xen](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/xen/32/2288_2.png) [@Xen](https://forum.crystal-lang.org/u/Xen)\
**Post date:** [April 21, 2023, 8:19pm UTC](https://forum.crystal-lang.org/t/extracting-singleton-functionality-into-modules/2162/5 "2023-04-21T20:19:58Z")

</div>

```auto
class Foo
  INSTANCE = new

  private def initialize
  end
end

```

Oh, that looks nice. But how does that work? In an interpreted language, that would create an instance as soon as loading the file, something that one would want to avoid. But I’m guessing that the compiler would notice if one didn’t use that constant, and optimize the initialization away, or?

---

<div class="post-metadata">

**Author:** ![Devonte](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/devonte/32/1927_2.png) [@Devonte](https://forum.crystal-lang.org/u/Devonte)\
**Post date:** [April 21, 2023, 10:00pm UTC](https://forum.crystal-lang.org/t/extracting-singleton-functionality-into-modules/2162/6 "2023-04-21T22:00:39Z")

</div>

Correct, code that isn’t used/referenced anywhere is not used, so in this case the constant wouldn’t be compiled.

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [April 22, 2023, 5:52am UTC](https://forum.crystal-lang.org/t/extracting-singleton-functionality-into-modules/2162/7 "2023-04-22T05:52:00Z")

</div>

> [@midnight-wonderer](#):
>
> However, when I tried extracting the functionality into a module, I’ll get a compilation error.

Hi, How you extract the functionality into module? could you please give code sample? And, what is the compilation error?

AFAIK, you code should just work.

```crystal
module M1
  private SINGLETON = ::Mutex.new
  @@instance : self?

  macro included
    def self.instance
      @@instance || SINGLETON.synchronize do
        @@instance ||= new
      end
    end
  end
end

class C1
  include M1
end

c1 = C1.instance # => #<C1:0x7fb698e1be60>
c2 = C1.instance # => #<C1:0x7fb698e1be60>

```

Although, as said by straight-shoota, code can be reduce to: (ignore make initialize private)

```crystal
module M1
  @@instance : self?

  macro included
    def self.instance
      @@instance ||= new
    end
  end
end

class C1
  include M1
end

p c1 = C1.instance # => #<C1:0x7fb698e1be60>
p c2 = C1.instance # => #<C1:0x7fb698e1be60>

```

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [April 22, 2023, 5:57am UTC](https://forum.crystal-lang.org/t/extracting-singleton-functionality-into-modules/2162/8 "2023-04-22T05:57:14Z")

</div>

> [@straight-shoota](#):
>
> Constants are guaranteed to be initialized only once, so instead of a class variable, you could just use a constant (being a signleton it won’t need to be reassigned anyway)

AFAIK, the class variable in Crystal(basically, the ruby equivalent of class instance variable, right?), can be guaranteed to be initialized only once for includeded class?(check following example) could you please give a example for initialized twice? thanks

```crystal
module M1
  @@instance : self?

  macro included
    def self.instance
      @@instance ||= new
    end
  end
end

class C1
  include M1
end

class C2
  include M1
end

p C1.instance # => #<C1:0x7fb698e1be60>
p C1.instance # => #<C1:0x7fb698e1be60>

p C2.instance # => #<C2:0x7fb698e1be70>
p C2.instance # => #<C2:0x7fb698e1be70>

```

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [April 24, 2023, 1:42pm UTC](https://forum.crystal-lang.org/t/extracting-singleton-functionality-into-modules/2162/9 "2023-04-24T13:42:01Z")

</div>

I have one more question hope the core team can answer me.

Why we can introduce (very specified) module like [OAuth Client](https://crystal-lang.org/api/1.8.1/OAuth2/Client.html) into standard lib, but, instead, a minimized implementation Singleton pattern could not be added?

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [April 24, 2023, 2:25pm UTC](https://forum.crystal-lang.org/t/extracting-singleton-functionality-into-modules/2162/10 "2023-04-24T14:25:01Z")

</div>

Because it’s unnecessary:

> [@straight-shoota](#):
>
> So the implementation of a singleton is actually quite trivial. IMO there’s no need for a generalized module to include this, when it’s just three loc:

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [April 24, 2023, 2:33pm UTC](https://forum.crystal-lang.org/t/extracting-singleton-functionality-into-modules/2162/11 "2023-04-24T14:33:53Z")

</div>

No, it is not trivial.

1. tree loc not limit call on initialize/new method.
2. you can still call #dup, #clone.

reference the ruby version [singleton](https://github.com/ruby/ruby/blob/master/lib/singleton.rb), there are more work to do.

The key point is, use singleton module just like Ruby (single included it) is a best practice, we should inherit Ruby goodies.

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [April 24, 2023, 8:09pm UTC](https://forum.crystal-lang.org/t/extracting-singleton-functionality-into-modules/2162/12 "2023-04-24T20:09:19Z")

</div>

Sure, it’s nothing fancy. Just a trivially minimal singleton implementation. That’s good enough for many use cases where people have asked for this.

I expect a more sophisticated implementation would be accepted into stdlib. There is an open feature request for this: [Singleton module · Issue #8846 · crystal-lang/crystal · GitHub](https://github.com/crystal-lang/crystal/issues/8846)  
However, little response to that issue indicates not a lot of interest from users. So nobody has picked it up yet. Contributions are welcome, but it’s not a priority for the Core Team.

---

<div class="post-metadata">

**Author:** ![Xen](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/xen/32/2288_2.png) [@Xen](https://forum.crystal-lang.org/u/Xen)\
**Post date:** [May 11, 2023, 7:23pm UTC](https://forum.crystal-lang.org/t/extracting-singleton-functionality-into-modules/2162/13 "2023-05-11T19:23:01Z")

</div>

It’s a philosophical question, innit?

While a more correct implementation that, for instance, prevents duping, can be considered more “pure”, what a lot of people are probably looking for when they say “singleton” is just a class that makes it easy to get that single instance without too much faffing about.

In that light @straight-shoota’s example is simple and elegant.

Of course, that doesn’t help much if you _really_ want to make sure that nobody ever clones your `INSTANCE`. I guess it could become a problem in big projects.

But one thing I’ve learned is that you don’t always need the proper textbook implementation of “a thing”. Often something simpler but with limitations works just as well, and can make things simpler. Sometimes you just extract data from HTML with regexps.
