# Specification of the signature of a Function in a struct

**URL:** <https://discourse.julialang.org/t/specification-of-the-signature-of-a-function-in-a-struct/25921>\
**Category:** New to Julia\
**Created:** [July 2, 2019, 10:45am UTC](https://discourse.julialang.org/t/specification-of-the-signature-of-a-function-in-a-struct/25921 "2019-07-02T10:45:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim\_Lavers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim_lavers/32/9097_2.png) [@Tim\_Lavers](https://discourse.julialang.org/u/Tim_Lavers)\
**Post date:** [July 2, 2019, 10:45am UTC](https://discourse.julialang.org/t/specification-of-the-signature-of-a-function-in-a-struct/25921/1 "2019-07-02T10:45:51Z")

</div>

Hi. I’ve got a struct that has a Function as a field. I’d like to specify the signature of this function in some way, for example:

```julia
struct Mapper
  lower::Float64
  upper::Float64
  mapFunc::Function{Float64 -> Float64}
end

```

Is there any way of doing this?  
Thanks.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 2, 2019, 11:13am UTC](https://discourse.julialang.org/t/specification-of-the-signature-of-a-function-in-a-struct/25921/2 "2019-07-02T11:13:47Z")

</div>

> [@Tim\_Lavers](#):
>
> Is there any way of doing this?

Depends on what you want to do with it, but generally not. If you are interested in the rationale and alternative solutions, search the discussions for “arrow types”.

If you just want concrete types, the idiomatic solution is

```julia
struct Mapper{T}
  lower::Float64
  upper::Float64
  mapFunc::T
end

```

---

<div class="post-metadata">

**Author:** ![Tim\_Lavers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim_lavers/32/9097_2.png) [@Tim\_Lavers](https://discourse.julialang.org/u/Tim_Lavers)\
**Post date:** [July 2, 2019, 11:56pm UTC](https://discourse.julialang.org/t/specification-of-the-signature-of-a-function-in-a-struct/25921/3 "2019-07-02T23:56:25Z")

</div>

Thanks! I’ll look into arrow types, as you suggest.

---

<div class="post-metadata">

**Author:** ![Bruno\_Amorim](https://avatars.discourse-cdn.com/v4/letter/b/f6c823/32.png) [@Bruno\_Amorim](https://discourse.julialang.org/u/Bruno_Amorim)\
**Post date:** [July 5, 2019, 4:10pm UTC](https://discourse.julialang.org/t/specification-of-the-signature-of-a-function-in-a-struct/25921/4 "2019-07-05T16:10:56Z")

</div>

Could a possible solution be to define a struct and a functor

```julia
struct TypedFunction{F<:Function, Tin, Tout}
    f::F
end

TypedFunction(f::F, ::Type{Tin}, ::Type{Tout}) where {F, Tin, Tout} = TypedFunction{F, Tin, Tout}(f)

function (typedfun::TypedFunction{F, Tin, Tout})(x::Tin) where {F, Tin, Tout}

    return convert(Tout, typedfun.f(x))
    
end

```

where `Tin` is the type of the input and `Tout` the type of the output and the functor guarantees that the input and the output are of the correct type.

In this way, if I define

`mysin = TypedFunction(sin, Float64, Float64)`

the compiler always know `mysin` only accepts arguments of the type `Float64` and the returned value of `mysin(2.0)` is always a `Float64`. Correct?

P.S:: This discussion is related to this problem: [Enforcing function signatures by both argument & return types](https://discourse.julialang.org/t/enforcing-function-signatures-by-both-argument-return-types/8174)

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [July 5, 2019, 5:10pm UTC](https://discourse.julialang.org/t/specification-of-the-signature-of-a-function-in-a-struct/25921/5 "2019-07-05T17:10:34Z")

</div>

> [@Bruno\_Amorim](#):
>
> mysin = TypedFunction(sin, Float64, Float64)

Actually, it does not.

For direct use, it works:

```julia
julia> @code_warntype(mysin(1.5))
Variables
  typedfun::Core.Compiler.Const(TypedFunction{typeof(sin),Float64,Float64}(sin), false)
  x::Float64

Body::Float64
1 ─ %1 = $(Expr(:static_parameter, 3))::Core.Compiler.Const(Float64, false)
│ %2 = Base.getproperty(typedfun, :f)::Core.Compiler.Const(sin, false)
│ %3 = (%2)(x)::Float64
│ %4 = Main.convert(%1, %3)::Float64
└── return %4

```

`sin` works for indirect use:

```julia

julia> function testsin(x::Float64)
          y = sin(x)
          return y
       end
testsin (generic function with 1 method)

julia> @code_warntype(testsin(0.5))
Variables
  #self#::Core.Compiler.Const(testsin, false)
  x::Float64
  y::Float64

Body::Float64
1 ─ (y = Main.sin(x))
└── return y

```

here is `mysin`

```julia

julia> function testmysin(x::Float64)
          y = mysin(x)
          return y
       end
testmysin (generic function with 1 method)

julia> @code_warntype(testmysin(0.5))
Variables
  #self#::Core.Compiler.Const(testmysin, false)
  x::Float64
  y::Any

Body::Any
1 ─ (y = Main.mysin(x))
└── return y

```

---

<div class="post-metadata">

**Author:** ![Bruno\_Amorim](https://avatars.discourse-cdn.com/v4/letter/b/f6c823/32.png) [@Bruno\_Amorim](https://discourse.julialang.org/u/Bruno_Amorim)\
**Post date:** [July 5, 2019, 5:41pm UTC](https://discourse.julialang.org/t/specification-of-the-signature-of-a-function-in-a-struct/25921/6 "2019-07-05T17:41:33Z")

</div>

Thanks for the feedback! That is interesting. But if I declare `mysin` as a `const`

```julia
const mysin = TypedFunction(sin, Float64, Float64)

function testmysin(x::Float64)
    y = mysin(x)
    return y
end

```

then it seems to work fine

```julia
julia> @code_warntype(testmysin(0.5))
Body::Float64
1 ─ %1 = invoke sin(_2::Float64)::Float64
└── return %1

```

I am not sure I understand why it works in this case…

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [July 5, 2019, 5:47pm UTC](https://discourse.julialang.org/t/specification-of-the-signature-of-a-function-in-a-struct/25921/7 "2019-07-05T17:47:22Z")

</div>

const values propagate well
