# Struct of singleton Abstract types performance

**URL:** <https://discourse.julialang.org/t/struct-of-singleton-abstract-types-performance/77679>\
**Category:** Performance\
**Tags:** struct\
**Created:** [March 10, 2022, 11:39am UTC](https://discourse.julialang.org/t/struct-of-singleton-abstract-types-performance/77679 "2022-03-10T11:39:28Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 10, 2022, 1:31pm UTC](https://discourse.julialang.org/t/struct-of-singleton-abstract-types-performance/77679/5 "2022-03-10T13:31:10Z")

</div>

For this specific case, why you do not use enumeration? Do you expect users to extend your abstract type?

Perceive that the cost is inescapable, if you used an `@Enum` or an `Int` directly to distinguish between the singleton types, then when you wanted to dispatch on these values you would need to wrap them in `Val(value)`. So any code that wants to dispatch on a a field that may store different types (disguised or not) needs to pay the price of type instability. The only ways out I see are:

1. _Do not have a single struct with a field that can hold multiple distinct types, have a parametrized struct instead._ This is, use `mutable struct MyStruct1{consin <: AbstractType}; x :: Int; end`. Dispatch on the type parameter, that will be type stable because it is always the same for the same _instance_ of your parametric type.
2. Use an implicit or explicit enumeration and then `Val` with the [ValSplit package](https://discourse.julialang.org/t/ann-valsplit-jl-compile-away-dynamic-dispatch-on-val-typed-arguments/77096).

---

_[View the full topic](https://discourse.julialang.org/t/struct-of-singleton-abstract-types-performance/77679)._
