# Parametrized function type?

**URL:** <https://discourse.julialang.org/t/parametrized-function-type/59708>\
**Category:** New to Julia\
**Tags:** question, type\
**Created:** [April 21, 2021, 3:08am UTC](https://discourse.julialang.org/t/parametrized-function-type/59708 "2021-04-21T03:08:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [April 21, 2021, 3:08am UTC](https://discourse.julialang.org/t/parametrized-function-type/59708/1 "2021-04-21T03:08:55Z")

</div>

Is it possible to specify the signature of a function?

```julia
# mycall : (A -> B) -> A -> B
mycall(f::Function{A,B}, a::A)::B where {A,B} = f(a)
ERROR: UndefVarError: A not defined

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [April 21, 2021, 3:23am UTC](https://discourse.julialang.org/t/parametrized-function-type/59708/2 "2021-04-21T03:23:07Z")

</div>

TLDR is no. Every function in Julia has it’s own type. The simple reason for this is that functions do not have specific input or output types. Consider `+` what are it’s input and output types?

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [April 21, 2021, 3:25am UTC](https://discourse.julialang.org/t/parametrized-function-type/59708/3 "2021-04-21T03:25:09Z")

</div>

> [@Oscar\_Smith](#):
>
> Consider `+` what are it’s input and output types?

I’d say, it’s an inductive type `Has{+}`, just like an extensible enum.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [April 21, 2021, 3:26am UTC](https://discourse.julialang.org/t/parametrized-function-type/59708/4 "2021-04-21T03:26:02Z")

</div>

Julia doesn’t have inductive types (or dependent types for that matter).

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [April 23, 2021, 1:09am UTC](https://discourse.julialang.org/t/parametrized-function-type/59708/5 "2021-04-23T01:09:48Z")

</div>

I tried to find a workaround. Why didn’t this work?

```julia
julia> f(x::Float64)::Float64 = 1.0
f (generic function with 1 method)

julia> let T = typeof(f); methods(T.instance).ms[1].sig.types[2] == Core.Compiler.return_type(T.instance, (methods(T.instance).ms[1].sig.types[2],)) end
true

# mycall : (A -> B) -> A -> B
julia> mycall(f::T,a::A) where {methods(T.instance).ms[1].sig.types[2] == Core.Compiler.return_type(T.instance, (methods(T.instance).ms[1].sig.types[2],)), A} = f(a)
ERROR: syntax: invalid variable expression in "where" around REPL[15]:1

```

EDIT: I think function calls aren’t allowed in `where` expressions.

```julia
mycall(f::T,a::A) where {g(T),A} = f(a)
ERROR: syntax: invalid variable expression in "where" around REPL[20]:1

```

---

<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:** [April 23, 2021, 1:33am UTC](https://discourse.julialang.org/t/parametrized-function-type/59708/6 "2021-04-23T01:33:32Z")

</div>

Aren’t functors useful there?

```julia
julia> struct F{A}
       end

julia> (f::F)(x) = sin(x)

julia> mycall(f::F{A}, a::A) where {A} = f(a)
mycall (generic function with 1 method)

julia> f = F{Int}()
F{Int64}()

julia> mycall(f,1)
0.8414709848078965

julia> mycall(f,1.0)
ERROR: MethodError: no method matching mycall(::F{Int64}, ::Float64)

```

I am not sure if there is something that can be done with the return value, as you seem to try in the example.
