# Best way to declare a structure field that could be a function or a number

**URL:** <https://discourse.julialang.org/t/best-way-to-declare-a-structure-field-that-could-be-a-function-or-a-number/56574>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [March 5, 2021, 4:37pm UTC](https://discourse.julialang.org/t/best-way-to-declare-a-structure-field-that-could-be-a-function-or-a-number/56574 "2021-03-05T16:37:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tiago](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tiago/32/22567_2.png) [@tiago](https://discourse.julialang.org/u/tiago)\
**Post date:** [March 5, 2021, 4:37pm UTC](https://discourse.julialang.org/t/best-way-to-declare-a-structure-field-that-could-be-a-function-or-a-number/56574/1 "2021-03-05T16:37:43Z")

</div>

Hello,  
I’m currently writing a PDE solver in julia, and I’m wrapping the inputs in a structure in order. Among these inputs is the initial condition `V0` and can be either a constant or a function.

My question is, what is the best way to write a structure with such field? I currently have the following implemented:

```julia
struct input{T,P<:AbstractFloat,O<:Signed}
    V0 :: T
     L :: P
     N :: O
end

```

Is this the correct approach to this use case? In this situation, the field `input.V0` is always well defined:

```julia
julia> f(x) = x^2;
julia> in_1 = input(0,5.5,55);
julia> in_2 = input(f,5.5,55);
julia> typeof(in_1.V0)
Int64
julia> typeof(in_2.V0)
typeof(f)

```

Am I correct? Sorry for the newbie question.

Best regards,  
Tiago

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [March 5, 2021, 5:26pm UTC](https://discourse.julialang.org/t/best-way-to-declare-a-structure-field-that-could-be-a-function-or-a-number/56574/2 "2021-03-05T17:26:42Z")

</div>

Yes, that’s how it’s usually handled.

But probably it’s best to go with function-only approach to avoid special cases, i.e.

```julia
struct Input{F<:Function,P<:AbstractFloat,O<:Signed}
    V0 :: F
     L :: P
     N :: O
end

Input(V0::Number, L, N) = Input(_->V0, float(L), Signed(N))

```

---

<div class="post-metadata">

**Author:** ![tiago](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tiago/32/22567_2.png) [@tiago](https://discourse.julialang.org/u/tiago)\
**Post date:** [March 5, 2021, 6:40pm UTC](https://discourse.julialang.org/t/best-way-to-declare-a-structure-field-that-could-be-a-function-or-a-number/56574/3 "2021-03-05T18:40:06Z")

</div>

Ok, so declaring the structure for the function case and then create a method to it to deal with the constant case, right? What did you mean about avoiding special cases? Sorry, I didn’t understand!

By the way, what means `Input(_->V0,...)`? It’s a kind of mapping or something?

Thanks!

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [March 5, 2021, 7:30pm UTC](https://discourse.julialang.org/t/best-way-to-declare-a-structure-field-that-could-be-a-function-or-a-number/56574/4 "2021-03-05T19:30:57Z")

</div>

> [@tiago](#):
>
> Ok, so declaring the structure for the function case and then create a method to it to deal with the constant case, right?

Right, so that you don’t need to write separate codes for `V0::Number` and `V0::Function` and just assume that `V0` is always a function in the code that uses the structure.

> [@tiago](#):
>
> By the way, what means `Input(_->V0,...)` ?

`_->V0` is an anonymous function of one argument which returns `V0` for any input. You may write it as `x->V0` or `function(x) V0 end`. The underscore just shows that the argument is in fact ignored. More verbosely, the same constructor is written as

```julia
function Input(V0::Number, L, N)
    function always_V0(x)
        return V0
    end
    return Input(always_V0, float(L), Signed(N))
end

```
