# Val types vs singletons, is one easier for inference / compilation?

**URL:** <https://discourse.julialang.org/t/val-types-vs-singletons-is-one-easier-for-inference-compilation/52599>\
**Category:** General Usage\
**Created:** [December 30, 2020, 2:37am UTC](https://discourse.julialang.org/t/val-types-vs-singletons-is-one-easier-for-inference-compilation/52599 "2020-12-30T02:37:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [December 30, 2020, 2:37am UTC](https://discourse.julialang.org/t/val-types-vs-singletons-is-one-easier-for-inference-compilation/52599/1 "2020-12-30T02:37:47Z")

</div>

I’ve got some quantity which is inherently a singleton, and a function which returns it as part of a tuple. If I denote the singleton quantity by a type itself, I lose the type information (as expected):

```julia
struct Singleton end
foo() = (Singleton, 1)
@code_warntype foo() # Body::Tuple{DataType,Int64}

```

Somehow I have to hoist the information into the type system. I see two ways. Either denote the quantity by _an instance_ of the type:

```julia
struct Singleton end
foo() = (Singleton(), 1)
@code_warntype foo() # Body::Tuple{Singleton,Int64}

```

or use a Val-type:

```julia
struct Singleton end
foo() = (Val(Singleton), 1)
@code_warntype foo() # Body::Tuple{Val{Singleton},Int64}

```

I’m wondering if in general one strategy or the other is easier on the compiler / on inference? From some benchmarks of my non-MWE version of this, I don’t really see any difference, but wondering if I might hit something as the code gets more complex (thinking along the lines of recursion limits, order-dependent inference issues, general slowness, etc…). Thanks for any insights.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 30, 2020, 2:46am UTC](https://discourse.julialang.org/t/val-types-vs-singletons-is-one-easier-for-inference-compilation/52599/2 "2020-12-30T02:46:09Z")

</div>

They should be about the same.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [December 30, 2020, 4:25am UTC](https://discourse.julialang.org/t/val-types-vs-singletons-is-one-easier-for-inference-compilation/52599/3 "2020-12-30T04:25:18Z")

</div>

You are better off without the `Val` here – just additional syntax without benefit.  
`Val` used to be used more often than it is nowadays. Julia’s internals improved substantively; some prior uses of `Val` are no longer impactful,

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [December 30, 2020, 5:52am UTC](https://discourse.julialang.org/t/val-types-vs-singletons-is-one-easier-for-inference-compilation/52599/4 "2020-12-30T05:52:40Z")

</div>

Of course you could mimic `nothing` and `missing` and do

```julia
const singleton = Singleton()

```

if you want syntax like your first example.
