# Naming of singleton types and their instance

**URL:** <https://discourse.julialang.org/t/naming-of-singleton-types-and-their-instance/8399>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 16, 2018, 1:44pm UTC](https://discourse.julialang.org/t/naming-of-singleton-types-and-their-instance/8399 "2018-01-16T13:44:42Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![helgee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgee/32/2022_2.png) [@helgee](https://discourse.julialang.org/u/helgee)\
**Post date:** [January 16, 2018, 1:44pm UTC](https://discourse.julialang.org/t/naming-of-singleton-types-and-their-instance/8399/1 "2018-01-16T13:44:42Z")

</div>

> [@Singleton types vs. instances as type parameters](https://discourse.julialang.org/t/singleton-types-vs-instances-as-type-parameters/2802):
>
> Let’s say I want to create a family of singleton types, and additional types that might use them as type parameters. The particular example I’m thinking of is a named Variable type, and a type to hold powers of that variable: struct Variable{Name} Variable{N}() where {N} = new{typeassert(N, Symbol)}() end I have a choice here: I can either use the variable type as a parameter, or I can use its singleton instance. That is, I can do this: struct Power{V \<: Variable} exponent::Int end or th…

Based on the discussion in the above thread I am refactoring my code to dispatch on instances instead of types (which also helps me to avoid some illegal stuff I was doing, e.g. overloading `Base.show` for types). But now I have a (silly) naming problem…

Let’s assume I have a singleton type representing the planet Mars:

```julia
struct Mars end

```

Now I have a function that returns its radius. In the past I would have written it like this

```julia
radius(::Type{Mars}) = 3389.5

```

which makes a nice looking call possible

```julia
radius(Mars)

```

If I want to use the instance instead this becomes

```julia
radius(::Mars) = 3389.5
radius(Mars()) # ugly

```

Not only do I think that the empty parens are ugly but also a potential source of errors for newcomers, e.g.

```julia
julia> radius(Mars)
ERROR: MethodError: no method matching radius(::Type{Mars})

```

## Possible solutions:

1. Do it like `Base`

```julia
struct Mars end
const mars = Mars()

```

This is not really satisfying because `Mars` is a proper noun and should start with a capital letter.

1. Add a `*Type` suffix

```julia
# Ugly name for the type
struct MarsType end
# Nice name for the instance
const Mars = MarsType()
# Hide the ugly type name from the user
Base.show(io::IO, ::MarsType) = print(io, "Mars")
# Ugly code
radius(::MarsType) = 3389.5
# Nice call
radius(Mars)

```

Also not really satisfying because I have to type `MarsType` a lot and it feels like an implementation detail leaking out.

_Any other ideas?_

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [January 16, 2018, 3:37pm UTC](https://discourse.julialang.org/t/naming-of-singleton-types-and-their-instance/8399/2 "2018-01-16T15:37:25Z")

</div>

> [@helgee](#):
>
> This is not really satisfying because Mars is a proper noun and should start with a capital letter.

The Julia convention is have instance-bindings in lower-case, irrespective of proper English spelling, eg `einstein = Physicist(name="Einstein")` would be correct. I think it would be fine to do the `const mars = Mars()` and then only export `mars`.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 16, 2018, 5:13pm UTC](https://discourse.julialang.org/t/naming-of-singleton-types-and-their-instance/8399/3 "2018-01-16T17:13:19Z")

</div>

Also, some [recommend uppercase constants](https://github.com/JuliaPraxis/Naming/blob/eed7048a46e9b796fe166e25348590415c9084af/guides/NamingGuide.md#our-conventions), so `const MARS = ...` would be OK too.

---

<div class="post-metadata">

**Author:** ![helgee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgee/32/2022_2.png) [@helgee](https://discourse.julialang.org/u/helgee)\
**Post date:** [January 16, 2018, 7:57pm UTC](https://discourse.julialang.org/t/naming-of-singleton-types-and-their-instance/8399/4 "2018-01-16T19:57:41Z")

</div>

Thanks! It seems the only problems here are my aesthetic sensibilities 😂

How would you handle the following case?

```julia
# 1
struct UTC <: TimeScale end
const utc = UTC() 

# 2
struct Utc <: TimeScale end
const UTC = Utc()

# 3
struct UTCScale <: TimeScale end
const UTC = UTCScale()
Base.show(io::IO, ::UTCScale) = print(io, "UTC")

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 17, 2018, 5:26am UTC](https://discourse.julialang.org/t/naming-of-singleton-types-and-their-instance/8399/5 "2018-01-17T05:26:38Z")

</div>

I would not use 2 for acronyms, they look better in all caps. Apart from that, both 1 and 3 are fine.

Not everyone makes constants all caps though, not even in `Base`, what I linked is just one of the recommendations.

I generally try not to worry about these things in the first pass too much, if a patter emerges when I refactor then I make things consistent.

---

<div class="post-metadata">

**Author:** ![Olivier\_Merchiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olivier_merchiers/32/4073_2.png) [@Olivier\_Merchiers](https://discourse.julialang.org/u/Olivier_Merchiers)\
**Post date:** [January 17, 2018, 10:07am UTC](https://discourse.julialang.org/t/naming-of-singleton-types-and-their-instance/8399/6 "2018-01-17T10:07:53Z")

</div>

Hi,

For the planet case I would do it slightly different:

```julia
abstract type AbstractPlanet end

struct Planet <: AbstractPlanet
    radius :: Float64
    mass :: Float64
end

```

Now Mars is just an instance of Planet:

```julia
Mars = Planet(3389.5 , 642e21)

```

Since every planet has a mass and radius, you can now dispatch on AbstractPlanet:

```julia
mass(planet::AbstractPlanet) = planet.mass
radius(planet::AbstractPlanet) = planet.radius

```

Then you get:

```julia
julia> radius(Mars)
3389.5 

```

This does not answer the question which naming convention is better, but personally this is my preferred way to organize such code.  
Like this you can dissociate the instance name from the struct’s name, and avoids creating a lot of singleton types.

For the timescale I would do something similar.  
Hope this helps.

---

<div class="post-metadata">

**Author:** ![helgee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgee/32/2022_2.png) [@helgee](https://discourse.julialang.org/u/helgee)\
**Post date:** [January 17, 2018, 10:21am UTC](https://discourse.julialang.org/t/naming-of-singleton-types-and-their-instance/8399/7 "2018-01-17T10:21:14Z")

</div>

> [@Olivier\_Merchiers](#):
>
> Now Mars is just an instance of Planet:

Thanks for the suggestion, but `Mars` being a singleton is non-negotiable for my implementation. I need this to be able to dispatch on the type like in this simplified example:

```julia
# I have a state vector type that has the central body as a type parameter
struct State{T} end

function State{S}(s::State{T}) where {S<:Planet, T<:Planet}
    # do the transformation
end

mars_state = State{Mars}(...)
# Magically transform the state from Mars-centered to Earth-centered
earth_state = State{Earth}(mars_state)

```

And I also like the fact that there cannot be competing instances of `Mars` this way 😉

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [January 17, 2018, 10:46am UTC](https://discourse.julialang.org/t/naming-of-singleton-types-and-their-instance/8399/8 "2018-01-17T10:46:23Z")

</div>

> [@Olivier\_Merchiers](#):
>
> personally this is my preferred way to organize such code.

Until some time ago I’d have agreed with you and this is how planets are defined in [`AstroLib.jl`](https://github.com/JuliaAstro/AstroLib.jl). However, I see the point of Helgee’s approach: if you want to add a property to a planet you don’t have to change the data structure and planet instances, but just define a new method, perhaps not necessary for all planets.

@helgee: we should probably have an `AstroBase.jl` package where to move this stuff and avoid code/numbers duplication 🙂

---

<div class="post-metadata">

**Author:** ![helgee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgee/32/2022_2.png) [@helgee](https://discourse.julialang.org/u/helgee)\
**Post date:** [January 17, 2018, 10:52am UTC](https://discourse.julialang.org/t/naming-of-singleton-types-and-their-instance/8399/9 "2018-01-17T10:52:40Z")

</div>

> [@giordano](#):
>
> @helgee: we should probably have an AstroBase.jl package where to move this stuff and avoid code/numbers duplication 🙂

👍 💯

---

<div class="post-metadata">

**Author:** ![Olivier\_Merchiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olivier_merchiers/32/4073_2.png) [@Olivier\_Merchiers](https://discourse.julialang.org/u/Olivier_Merchiers)\
**Post date:** [January 17, 2018, 11:07am UTC](https://discourse.julialang.org/t/naming-of-singleton-types-and-their-instance/8399/10 "2018-01-17T11:07:55Z")

</div>

> [@giordano](#):
>
> I see the point of Helgee’s approach: if you want to add a property to a planet you don’t have to change the data structure and planet instances, but just define a new method, perhaps not necessary for all planets.

Yeah, I totally agree this is a powerful way to extend behaviour and I wouldn’t know how to do this in general with the “struct Planet” approach.

On the other hand, if I want to add behaviour, I find it quite easy to add a field to the Planet struct and then write an outer constructor such that the old code still works. I think this was explained in the discussion linked by Helgee.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [January 17, 2018, 3:50pm UTC](https://discourse.julialang.org/t/naming-of-singleton-types-and-their-instance/8399/11 "2018-01-17T15:50:18Z")

</div>

> [@Olivier\_Merchiers](#):
>
> On the other hand, if I want to add behaviour, I find it quite easy to add a field to the Planet struct and then write an outer constructor such that the old code still works. I think this was explained in the discussion linked by Helgee.

[GitHub - mauro3/Parameters.jl: Types with default field values, keyword constructors and (un-)pack macros](https://github.com/mauro3/Parameters.jl) often can help with that by allowing to specify a default for a newly added field.
