# Trait definitions: type level vs instance level

**URL:** <https://discourse.julialang.org/t/trait-definitions-type-level-vs-instance-level/103587>\
**Category:** General Usage\
**Created:** [September 6, 2023, 9:16pm UTC](https://discourse.julialang.org/t/trait-definitions-type-level-vs-instance-level/103587 "2023-09-06T21:16:03Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [September 7, 2023, 12:55pm UTC](https://discourse.julialang.org/t/trait-definitions-type-level-vs-instance-level/103587/3 "2023-09-07T12:55:31Z")

</div>

I think this is a matter of taste. Related thread:

> [@Should I dispatch on singleton types or their instances?](https://discourse.julialang.org/t/should-i-dispatch-on-singleton-types-or-their-instances/28204):
>
> When I am developing packages, I often come across the following problem. struct Step{T} end func(::Step{1}, x) = do something... func(::Step{2}, x) = do some different things... I am not sure should I use singleton types Type{Step{1}} and Type{Step{2}}, instead of singletons Step{1} and Step{2} to dispatch behaviors? func(::Type{Step{1}}, x) = do something... func(::Type{Step{2}}, x) = do some different things... Julia’s doc says [instances should be used](https://docs.julialang.org/en/v1/manual/types/#%22Value-types%22-1), is it a general rule or just for V…

> [@mrufsvold](#):
>
> my guess is that you can always get a type from an instance with `typeof` but it could be potentially quite expensive to get an instance from a type

Getting the instance of a singleton type is free. `T.instance` currently works, though it’s not documented or part of the public interface as far as I know. A guaranteed-to-work way is by relying on [incomplete](https://docs.julialang.org/en/v1/manual/constructors/#Incomplete-Initialization) initialization:

```julia
struct Helper{T}
  v::T
  Helper{T}() where {T} = new{T}()
end
uninitialized_instance(::Type{T}) where {T} = Helper{T}().v

```

```julia-repl
julia> uninitialized_instance(Nothing) == nothing
true

```

This is a bit roundabout, but could be abstracted into a tiny package easily. I actually wanted to [register](https://github.com/JuliaRegistries/General/pull/76530) such a package, but then gave up because the functionality seemed overly trivial so I wasn’t sure that anyone would use the package.

---

_[View the full topic](https://discourse.julialang.org/t/trait-definitions-type-level-vs-instance-level/103587)._
