# Avoiding certain abstract types in the fields of struct

**URL:** https://discourse.julialang.org/t/avoiding-certain-abstract-types-in-the-fields-of-struct/108858
**Category:** New to Julia
**Tags:** question
**Created:** [January 16, 2024, 9:10am UTC](https://discourse.julialang.org/t/avoiding-certain-abstract-types-in-the-fields-of-struct/108858 "2024-01-16T09:10:52Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Maybe](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Maybe](https://discourse.julialang.org/u/Maybe)
#### Post date: [January 16, 2024, 9:10am UTC](https://discourse.julialang.org/t/avoiding-certain-abstract-types-in-the-fields-of-struct/108858/1 "2024-01-16T09:10:53Z")

</div>

I am trying to define a struct type for an algebraic functional form that has two “irregular” fields:

```julia
struct my_struct
     vars::NTuple{N, Int} where N
     func::Union{Function, Nothing}
     ...
end

```

The `vars` field includes the variable indices that are in the function (and hence it should have a variable length). The `func` field includes the function defined on those variables, but it should also be possible that there is no function assignment for the created object.

Based on my understanding, this is not a recommended format for defining these fields due to involving Abstract Types. I would appreciate any guidance on the best way to define these two fields.

---

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [January 16, 2024, 9:18am UTC](https://discourse.julialang.org/t/avoiding-certain-abstract-types-in-the-fields-of-struct/108858/2 "2024-01-16T09:18:09Z")

</div>

You can simply put those types as type parameters onto `my_struct` like so:

```julia
struct my_struct{N,F}
     vars::NTuple{N, Int}
     func::F
     ...
end

```

Beware that this is actually a tradeoff! If you have many many many combinations of `{N,F}` then you will see a lot of compilation time because Julia specializes each function for the input types. So putting things into the type parameters is only worth it if you use a specific instance for long enough to amortize the associated compilation cost.

See also [the relevant performance tip](https://docs.julialang.org/en/v1/manual/performance-tips/#The-dangers-of-abusing-multiple-dispatch-(aka,-more-on-types-with-values-as-parameters))

---

<div class="post-metadata">

### Author: ![Maybe](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Maybe](https://discourse.julialang.org/u/Maybe)
#### Post date: [January 16, 2024, 3:37pm UTC](https://discourse.julialang.org/t/avoiding-certain-abstract-types-in-the-fields-of-struct/108858/3 "2024-01-16T15:37:47Z")

</div>

Thanks a lot for the tip! I was also wondering if defining `F` as a subclass of `Union{Function, Nothing}` would have any benefits in this case?

```julia
struct my_struct{N,F<:Union{Function, Nothing}}
     vars::NTuple{N, Int}
     func::F
     ...
end

```

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [January 16, 2024, 3:44pm UTC](https://discourse.julialang.org/t/avoiding-certain-abstract-types-in-the-fields-of-struct/108858/4 "2024-01-16T15:44:59Z")

</div>

That would prevent an instance to be constructed with another type than those. But note that specifying the field inside the struct can be different than with the parametric type. Specifically, for example, if the struct is mutable, in one case you can mutate the value from one type to the other:

```julia-repl
julia> mutable struct M
           x::Union{Nothing, Int}
       end

julia> m = M(1)
M(1)

julia> m.x = nothing

julia> m
M(nothing)

```

while in this case you cannot:

```julia-repl
julia> mutable struct M2{T<:Union{Nothing,Int}}
           x::T
       end

julia> m2 = M2(1)
M2{Int64}(1)

julia> m2.x = nothing
ERROR: MethodError: Cannot `convert` an object of type Nothing to an object of type Int64

```

---

<div class="post-metadata">

### Author: ![Maybe](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Maybe](https://discourse.julialang.org/u/Maybe)
#### Post date: [January 16, 2024, 3:58pm UTC](https://discourse.julialang.org/t/avoiding-certain-abstract-types-in-the-fields-of-struct/108858/5 "2024-01-16T15:58:38Z")

</div>

Oh I see! Thank you for the note!
