# Singleton Types

**URL:** <https://discourse.julialang.org/t/singleton-types/96817>\
**Category:** Internals & Design\
**Created:** [March 30, 2023, 2:09am UTC](https://discourse.julialang.org/t/singleton-types/96817 "2023-03-30T02:09:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![olynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olynch/32/17839_2.png) [@olynch](https://discourse.julialang.org/u/olynch)\
**Post date:** [March 30, 2023, 2:09am UTC](https://discourse.julialang.org/t/singleton-types/96817/1 "2023-03-30T02:09:35Z")

</div>

Often I want to use zero-sized structs for dispatch purposes, and it’s kind of annoying to have to make up a name for both the struct and the unique instance of that struct.

In Scala, they have `object` as a primitive, which creates a class and the unique instance of it in one go. The class is then anonymous, and printed out as `Foo.type` (or something like that, I can’t remember exactly what).

Would something like this be possible in Julia? It wouldn’t be that hard to do with a macro, using `gensym` for the name of the struct; but then would it be possible to overload printing out the name of the type to be something more informative than the `gensym` name?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [March 30, 2023, 2:35am UTC](https://discourse.julialang.org/t/singleton-types/96817/2 "2023-03-30T02:35:32Z")

</div>

If your singleton type is `struct Foo end` then the name of the unique instance of it is `Foo()`. Seems short enough, no?

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [March 30, 2023, 3:00am UTC](https://discourse.julialang.org/t/singleton-types/96817/3 "2023-03-30T03:00:52Z")

</div>

> [@olynch](#):
>
> Would something like this be possible in Julia?

Already is—because functions are singleton objects (although it’s a bit awkward for this):

```julia
julia> function foo end
foo (generic function with 0 methods)

julia> f(x) = x
       f(::typeof(foo)) = "Foo!"
f (generic function with 2 methods)

julia> f(foo)
"Foo!"

```

> [@olynch](#):
>
> would it be possible to overload printing out the name of the type to be something more informative than the `gensym` name?

For with Julia, all things are possible:

```julia
julia> macro singleton(s::Symbol) sT=Symbol("#$s"); esc(quote
           struct $sT end
           Base.show(io::IO, ::$sT) = print(io, $("$s"))
           const $s = $sT()
       end) end
@singleton (macro with 1 method)

julia> @singleton bar
bar

julia> f(::typeof(bar)) = "Bar?"
f (generic function with 3 methods)

julia> f(bar)
"Bar?"

```

All that said, I’ve never found a reason not to just follow this:

> [@StefanKarpinski](#):
>
> If your singleton type is `struct Foo end` then the name of the unique instance of it is `Foo()`. Seems short enough, no?

```julia
julia> struct Baz end

julia> f(::Baz) = "Baz 😉"
f (generic function with 4 methods)

julia> f(Baz())
"Baz 😉"

```

This aligns more closely with how non-function singletons are already used throughout the language—meaning there’s less surprise, so unless there’s a good reason I’d stick with this.

---

<div class="post-metadata">

**Author:** ![olynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olynch/32/17839_2.png) [@olynch](https://discourse.julialang.org/u/olynch)\
**Post date:** [March 30, 2023, 4:14am UTC](https://discourse.julialang.org/t/singleton-types/96817/4 "2023-03-30T04:14:15Z")

</div>

When I see something called with parentheses, I assume that it could be _doing_ something, which scares me. But maybe I just have to get over that 😛.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 30, 2023, 5:19am UTC](https://discourse.julialang.org/t/singleton-types/96817/5 "2023-03-30T05:19:26Z")

</div>

When you see a capitalized name calling something with parentheses, you may assume it’s a constructor, which returns an object. If there are no input arguments, it’s quite likely a singleton (though it could, admittedly, be a non-singleton with default parameters values.)

I don’t believe it’s good practice to have capitalized functions that have observable side effects, if that’s what you are thinking.

> [@olynch](#):
>
> it’s kind of annoying to have to make up a name for both the struct and the unique instance of that struct.

But if you _do_ need this, isn’t that very straightforward?

```julia
const mysingleton = MySingleton() 

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [March 30, 2023, 12:18pm UTC](https://discourse.julialang.org/t/singleton-types/96817/6 "2023-03-30T12:18:44Z")

</div>

I think it’s that lowercase name there that the objection is to.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 30, 2023, 1:02pm UTC](https://discourse.julialang.org/t/singleton-types/96817/7 "2023-03-30T13:02:43Z")

</div>

It’s not an undue mental burden, is what I meant (and is what I thought was the problem). It is a little bit of typing, of course.
