# Struct Parametric struct with type constrains in Union

**URL:** <https://discourse.julialang.org/t/struct-parametric-struct-with-type-constrains-in-union/46854>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 18, 2020, 3:55pm UTC](https://discourse.julialang.org/t/struct-parametric-struct-with-type-constrains-in-union/46854 "2020-09-18T15:55:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![efokken](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@efokken](https://discourse.julialang.org/u/efokken)\
**Post date:** [September 18, 2020, 3:55pm UTC](https://discourse.julialang.org/t/struct-parametric-struct-with-type-constrains-in-union/46854/1 "2020-09-18T15:55:16Z")

</div>

Hi there!

I would like to use a struct of the following form:

```julia
struct thing{T<:Function}
     p::Union{Float64,T}
     someother_member
end

```

When I create an instance like this:

```julia
thing(x->2,"other")

```

it works well. But when I try the other one:

```julia
thing(4.0,"other")

```

I get the error `ERROR: UndefVarError: T not defined`

Is there another way to constrain my struct member to be either a float or a function?  
Even better would be to constrain it to a function from R to R, but thats a different problem.

---

<div class="post-metadata">

**Author:** ![WschW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wschw/32/6575_2.png) [@WschW](https://discourse.julialang.org/u/WschW)\
**Post date:** [September 18, 2020, 4:00pm UTC](https://discourse.julialang.org/t/struct-parametric-struct-with-type-constrains-in-union/46854/2 "2020-09-18T16:00:26Z")

</div>

Try something like:

```julia
struct thing{T <: Union{Function, Float64}}
     p::T
     someother_member
end

```

---

<div class="post-metadata">

**Author:** ![efokken](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@efokken](https://discourse.julialang.org/u/efokken)\
**Post date:** [September 18, 2020, 4:11pm UTC](https://discourse.julialang.org/t/struct-parametric-struct-with-type-constrains-in-union/46854/3 "2020-09-18T16:11:24Z")

</div>

Thanks for the fast answer!  
The drawback seems to be that the resulting type changes, if I understand it correctly depending on what concrete type I use in each `thing`.

Is the following solution I found better?  
(Taken from [different topic](https://discourse.julialang.org/t/having-a-little-trouble-with-recursively-defined-structs/22285) )

```julia
struct thin{T}
    p::Union{Float64,T}
    someother_member
    function thin{T}(p,someother_member) where {T}
        T <: Function || throw(ArgumentError("invalid type"))
        new{T}(p,someother_member)
    end  
end

```

I can call every type of `p` via `thing{Function}(p0,some0)` and it has the type

```julia
thing{function}

```

I just don’t know whether that is a performance advantage.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [September 18, 2020, 5:23pm UTC](https://discourse.julialang.org/t/struct-parametric-struct-with-type-constrains-in-union/46854/4 "2020-09-18T17:23:53Z")

</div>

I’m curious about the context where this problem arises.

You want to dispatch on a specific function `T` that is sometimes stored in `thin` but not always.

Generically, when I see a `Union{Float64,Function}` I wonder whether there is a cleaner approach.

If you have different “classes” of `thing` that are meant to dispatch to different methods of a given function, perhaps a Trait would be the way to go?

---

<div class="post-metadata">

**Author:** ![efokken](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@efokken](https://discourse.julialang.org/u/efokken)\
**Post date:** [September 18, 2020, 7:20pm UTC](https://discourse.julialang.org/t/struct-parametric-struct-with-type-constrains-in-union/46854/5 "2020-09-18T19:20:20Z")

</div>

The idea is that the struct contains a function. But sometimes the function is a certain type of function, for example linear.  
Then I replace the function by its parameterization, so that later on, I have the information available and can use tailored methods for the “high-structure function”.

Now writing it down, I have the feeling that I should create a subtype of `Function`, so that the special kind of function is still a function…  
Thank you for making me think about it!
