# Enum or struct?

**URL:** <https://discourse.julialang.org/t/enum-or-struct/90326>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 16, 2022, 5:22am UTC](https://discourse.julialang.org/t/enum-or-struct/90326 "2022-11-16T05:22:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [November 16, 2022, 5:22am UTC](https://discourse.julialang.org/t/enum-or-struct/90326/1 "2022-11-16T05:22:28Z")

</div>

I would have chosen **1** but finance packages I’ve seen do something like **2**.  
Is this better Julia ? more functional ?

**Option 1 - using enum**

```julia
@enum DayCount Actual360 Actual365 

Year_Fraction(d1::Date,d2::Date,dc::DayCount) =   
    @Match.match dc begin
        Actual360 => (d2 - d1)/Days(360)
        Actual365 => (d2 - d1)/Days(365)
end

```

**Option 2 - using types**

```julia
abstract type Day_Count end

struct Actual_360 <: Day_Count end
struct Actual_365 <: Day_Count end

Year_Fraction(d1::Date,d2::Date, ::Actual_360) = (d2 - d1)/Day(360)
Year_Fraction(d1::Date,d2::Date, ::Actual_365) = (d2 - d1)/Day(365)

```

---

<div class="post-metadata">

**Author:** ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)\
**Post date:** [November 16, 2022, 11:33am UTC](https://discourse.julialang.org/t/enum-or-struct/90326/2 "2022-11-16T11:33:41Z")

</div>

When we type

```julia
julia> @macroexpand @enum DayCount Actual360 Actual365

```

in REPL, we can see that a primitive type is created:

```julia
primitive type DayCount <: Base.Enums.Enum{Int32} 32 end

```

and a dictionary related

```julia
 Dict{Int32, Symbol}(0 => :Actual360, 1 => :Actual365)

```

and at the end, it is still about the types. It seems Option 2 has less computation overhead.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [November 16, 2022, 11:41am UTC](https://discourse.julialang.org/t/enum-or-struct/90326/3 "2022-11-16T11:41:32Z")

</div>

While enum’s use types, they are still pretty different than the 2nd option. The first option is making a single (primitive) type representing all `Day_Count`’s, and then using two _values_ of that type to represent different counts. The 2nd option is making _separate_ types for each `Day_Count`.

One way to see the difference is: do you want the compiler to specialize on which `Day_Count` it is? And do you want type instability if it cannot infer which `Day_Count` it is? Both of those correspond to the 2nd option, since it is using separate types to represent the counts. Whether or not it is a good option depends on how it will be used.

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [November 17, 2022, 12:20am UTC](https://discourse.julialang.org/t/enum-or-struct/90326/4 "2022-11-17T00:20:43Z")

</div>

In reality there are 10 or so Day-Count types. I believe they are only used in a Year-Fraction function like the one shown.

I guess with Option 2 (as a struct) you can use a match statement or a method for each type. Whereas Option 1 limits you to the match statement.

As Professor Mehmet says Option 2 has less overhead.

enums have the advantage, when importing data from CSV, a string can be converted to an enum via |\> Symbol |\> eval. Its not so easy with struct types.
