<h2>Background</h2>
<p>The <a href="https://en.wikipedia.org/wiki/Singleton_pattern" rel="nofollow noreferrer">singleton pattern</a> is a widely used design pattern in the OOP language.</p>
<p>I'm considering adding a built-in singleton declaration to my language.</p>
<p>In Scala and Kotlin, we can use <code>object</code> to directly declare a singleton.</p>
<p>The advantage is that users can write less boilerplate code, and even thread bugs are hidden in the various template codes.</p>
<p>But my language is not strictly OOP, I also support global variables and lazy initialization, is it superfluous to use singleton mode at this time?</p>
<h2>Question</h2>
<p>Does the singleton pattern have advantages over global variables?</p>
<p>For what reasons do languages add built-in singleton keywords?</p>

<syntax><design-patterns>
Is it necessary to provide singleton as language keywords?
<h2>Singletons can be lazily initialized</h2>
<p>An object in Kotlin is always created when it is declared. It will consume RAM and time to construct regardless of if it is used. Tree shaking can maybe remove the declaration if it never used at all but not if it's called in some code branch and that branch is never reached.</p>
<p>Singletons on the other hand are lazy initialized. If they need some complex computation to construct or use a lot of memory or file handles or other rare resources, they won't use them till they are needed. This can be an advantage in certain situations.</p>
<p>Of course, the lazy behavior can be emulated with mutable globals without needing a specialized singleton keyword. You may consider most cases can be easily handled using <code>object</code> declarations that it's not worth creating an entire new syntax for something that would cost 2-5 lines of code to emulate.</p>
<p>It would also be quite easy to create a library to emulate a singleton feature based on simple object literals. You could consider simply adding such a function to the standard library instead of creating a specialized syntax for it. For example Rust the <a href="https://docs.rs/once_cell/latest/once_cell/" rel="nofollow noreferrer"><code>once_cell</code></a> is scheduled to be added to the standard library soon.</p>

CC BY-SA 4.0
2023-07-04T08:14:34.303Z
2023-07-04T14:01:05.487Z
2023-07-04T14:01:05.487Z
mousetail 'he-him'
CC BY-SA 4.0
2023-07-04T13:34:45.053Z
Note that the singleton pattern takes *more* than 2 lines of code; 3 lines in Kotlin, 5 in Java (excluding braces and empty lines)
Seggan
CC BY-SA 4.0
2023-07-04T14:00:54.397Z
In languages that support `??` you can do `static Self getInstance() {return Self::instance??=new Instance(..)}` for 2 lines. If you don't have such an operator it would add more.
mousetail 'he-him'
CC BY-SA 4.0
2023-07-04T14:32:20.467Z
A general way to declare a variable initialization as lazy might not be that useless of a syntax feature in general.
Philipp
<h2>It depends (yes, I know... it always depends.)</h2>
<p>The singleton pattern, when implemented manually in the code, allows one to customize <strong>how</strong> and <strong>when</strong> the initial creation takes place, and how multi-threading is handled (if it is).</p>
<p>On the other hand, providing singletons through a keyword gives to the user only one particular built-in implementation.</p>
<p>This might be good if you want that implementation to do something in particular, maybe in relation with some other features of your language.</p>
<p>Otherwise, it depends whether you expect that users may need to customize the provided implementation. This in turn  depends on the design goals of your language.</p>
<p>Languages oriented towards high-performance or low-level tuning might want to let the user do exactly what's right for them.</p>
<p>Languages with a more high-level perspective or DSLs might want to relieve the user from having to think about it and provide them with a good and easy to use default.</p>

CC BY-SA 4.0
2023-07-04T10:54:53.373Z
2023-07-04T10:54:53.373Z
Eldritch Conundrum
# Is it necessary to provide singleton as language keywords?
Posted by: username_0 on 2023-07-04
## Background

The [singleton pattern](https://en.wikipedia.org/wiki/Singleton_pattern) is a widely used design pattern in the OOP language.

I'm considering adding a built-in singleton declaration to my language.

In Scala and Kotlin, we can use `object` to directly declare a singleton.

The advantage is that users can write less boilerplate code, and even thread bugs are hidden in the various template codes.

But my language is not strictly OOP, I also support global variables and lazy initialization, is it superfluous to use singleton mode at this time?

## Question

Does the singleton pattern have advantages over global variables?

For what reasons do languages add built-in singleton keywords?
**Tags:** syntax design-patterns
---
## 2 answers
### Answer by username_4 on 2023-07-04. Score: 3
## It depends (yes, I know... it always depends.)

The singleton pattern, when implemented manually in the code, allows one to customize **how** and **when** the initial creation takes place, and how multi-threading is handled (if it is).

On the other hand, providing singletons through a keyword gives to the user only one particular built-in implementation.

This might be good if you want that implementation to do something in particular, maybe in relation with some other features of your language.

Otherwise, it depends whether you expect that users may need to customize the provided implementation. This in turn depends on the design goals of your language.

Languages oriented towards high-performance or low-level tuning might want to let the user do exactly what's right for them.

Languages with a more high-level perspective or DSLs might want to relieve the user from having to think about it and provide them with a good and easy to use default.
---
### Answer by username_1 on 2023-07-04. Score: 2
## Singletons can be lazily initialized

An object in Kotlin is always created when it is declared. It will consume RAM and time to construct regardless of if it is used. Tree shaking can maybe remove the declaration if it never used at all but not if it's called in some code branch and that branch is never reached.

Singletons on the other hand are lazy initialized. If they need some complex computation to construct or use a lot of memory or file handles or other rare resources, they won't use them till they are needed. This can be an advantage in certain situations.

Of course, the lazy behavior can be emulated with mutable globals without needing a specialized singleton keyword. You may consider most cases can be easily handled using `object` declarations that it's not worth creating an entire new syntax for something that would cost 2-5 lines of code to emulate.

It would also be quite easy to create a library to emulate a singleton feature based on simple object literals. You could consider simply adding such a function to the standard library instead of creating a specialized syntax for it. For example Rust the [`once_cell`](https://docs.rs/once_cell/latest/once_cell/) is scheduled to be added to the standard library soon.
> **Comments:**
> **username_2** (2023-07-04): Note that the singleton pattern takes *more* than 2 lines of code; 3 lines in Kotlin, 5 in Java (excluding braces and empty lines)  
> **username_1** (2023-07-04): In languages that support `??` you can do `static Self getInstance() {return Self::instance??=new Instance(..)}` for 2 lines. If you don't have such an operator it would add more.  
> **username_3** (2023-07-04): A general way to declare a variable initialization as lazy might not be that useless of a syntax feature in general.  
---
