# Can I get the declared return type of a method?

**URL:** <https://discourse.julialang.org/t/can-i-get-the-declared-return-type-of-a-method/43575>\
**Category:** Internals & Design\
**Created:** [July 23, 2020, 6:06pm UTC](https://discourse.julialang.org/t/can-i-get-the-declared-return-type-of-a-method/43575 "2020-07-23T18:06:11Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![bobcassels](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bobcassels/32/14586_2.png) [@bobcassels](https://discourse.julialang.org/u/bobcassels)\
**Post date:** [July 23, 2020, 6:06pm UTC](https://discourse.julialang.org/t/can-i-get-the-declared-return-type-of-a-method/43575/1 "2020-07-23T18:06:11Z")

</div>

```julia
f(a :: Int8, b :: Float32) :: Float64 = a + b

```

I have found ways to get the types of arguments of a method, such as `(first(methods(f))).sig`. But I don’t see a way to get the return type I declared. Is there / could there be a way?

---

<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:** [July 23, 2020, 7:08pm UTC](https://discourse.julialang.org/t/can-i-get-the-declared-return-type-of-a-method/43575/2 "2020-07-23T19:08:43Z")

</div>

Given that this value isn’t guaranteed to exist, it’s hard to imagine a place where this is what you want to do.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 23, 2020, 7:11pm UTC](https://discourse.julialang.org/t/can-i-get-the-declared-return-type-of-a-method/43575/3 "2020-07-23T19:11:21Z")

</div>

While you are correct, I do think that most methods are called with the expectation that they will return someday. Also, would not be hard to have a special type to be declared as return type of methods that never return (or just default to `nothing`).

---

<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:** [July 23, 2020, 7:14pm UTC](https://discourse.julialang.org/t/can-i-get-the-declared-return-type-of-a-method/43575/4 "2020-07-23T19:14:11Z")

</div>

What I meant is that for example the function `x()=rand()>.5? 1: 1.0` doesn’t have 1 return type. It either returns an `Int` or `Float64`, but you can’t tell from the method signature. (also `Union{Int, Float64}` would be incorrect since that is not a concrete type so no variable can have that type).

It is also impossible to determine if a given method ever returns.

---

<div class="post-metadata">

**Author:** ![Mark\_Nahabedian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_nahabedian/32/16562_2.png) [@Mark\_Nahabedian](https://discourse.julialang.org/u/Mark_Nahabedian)\
**Post date:** [July 23, 2020, 7:47pm UTC](https://discourse.julialang.org/t/can-i-get-the-declared-return-type-of-a-method/43575/5 "2020-07-23T19:47:21Z")

</div>

Bob posted thus question on my behalf.

I’m writing a program that uses constraint propagation to solve a puzzle. The program cycles through a set of “rule functions” until the puzzle is solved or no progress is made. All rule functions have the same argument signature and the same return type, and no other functions in my program will have the same argument and return signatures.

Rather than having to add each rule function to a collection when it’s defined, I thought I could use Julia’s introspection capabilities to discover them. I prefer this approach since Julia is annoyingly sensitive to definition order.

I understand that in many cases the return type of a method might be ambiguous. I am only concerned about the method’s declared return type, which Julia presumably stores someplace.

Thanks.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 23, 2020, 7:50pm UTC](https://discourse.julialang.org/t/can-i-get-the-declared-return-type-of-a-method/43575/6 "2020-07-23T19:50:01Z")

</div>

Oh, ok. But `(first(methods(f))).sig` may not return an abstract type too? As you can define some parameter as `Union`, `Any`, or `Abstract...`? Seems that in the context of this question an abstract type would be fine too.

---

<div class="post-metadata">

**Author:** ![bobcassels](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bobcassels/32/14586_2.png) [@bobcassels](https://discourse.julialang.org/u/bobcassels)\
**Post date:** [July 23, 2020, 7:53pm UTC](https://discourse.julialang.org/t/can-i-get-the-declared-return-type-of-a-method/43575/7 "2020-07-23T19:53:23Z")

</div>

I carefully posed the problem as finding the declared type, if any. I don’t expect to get an inferred type.

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [July 23, 2020, 7:56pm UTC](https://discourse.julialang.org/t/can-i-get-the-declared-return-type-of-a-method/43575/8 "2020-07-23T19:56:08Z")

</div>

Julia would be much happier giving you the inferred type ;). The declared type isn’t a real thing. It’s a syntactic aid that lowers `return x` to `return convert(T, x)`. In particular, it doesn’t even need to be a real type:

```julia
julia> foo(x, y)::typeof(x + y) = 1
foo (generic function with 1 method)

julia> foo(1, 1)
1

julia> foo(1, 1.0)
1.0

```

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [July 23, 2020, 7:58pm UTC](https://discourse.julialang.org/t/can-i-get-the-declared-return-type-of-a-method/43575/9 "2020-07-23T19:58:43Z")

</div>

Regardless, where the return type is simple as in your example, asking for the inferred type and the declared type will be the same thing (unless the function is known to error), so maybe just try that.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [July 23, 2020, 9:48pm UTC](https://discourse.julialang.org/t/can-i-get-the-declared-return-type-of-a-method/43575/10 "2020-07-23T21:48:46Z")

</div>

> [@Mark\_Nahabedian](#):
>
> I prefer this approach since Julia is annoyingly sensitive to definition order.

I’m curious what this is in reference to. Julia’s method semantics are carefully designed so that definition order does not matter.

---

<div class="post-metadata">

**Author:** ![Mark\_Nahabedian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_nahabedian/32/16562_2.png) [@Mark\_Nahabedian](https://discourse.julialang.org/u/Mark_Nahabedian)\
**Post date:** [July 24, 2020, 1:02am UTC](https://discourse.julialang.org/t/can-i-get-the-declared-return-type-of-a-method/43575/11 "2020-07-24T01:02:47Z")

</div>

The language implementation has other definition order issues, for example using a type in a declaration before it is defined. I understand done cases if thus have been resolved, but I still experience such problems in the newest release.

Also, if I were to define a global collection of my constraint functions, I expect it’s definition could not appear before all of those functions are defined.

---

<div class="post-metadata">

**Author:** ![Mark\_Nahabedian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_nahabedian/32/16562_2.png) [@Mark\_Nahabedian](https://discourse.julialang.org/u/Mark_Nahabedian)\
**Post date:** [July 24, 2020, 1:10am UTC](https://discourse.julialang.org/t/can-i-get-the-declared-return-type-of-a-method/43575/12 "2020-07-24T01:10:13Z")

</div>

Keno, you didn’t say, how would I enquire of Julia to happily provide me the inferred return type?

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [July 24, 2020, 2:10am UTC](https://discourse.julialang.org/t/can-i-get-the-declared-return-type-of-a-method/43575/13 "2020-07-24T02:10:29Z")

</div>

```julia
julia> Core.Compiler.return_type(x -> sin(x), (Int, ))
Float64

```

---

<div class="post-metadata">

**Author:** ![Mark\_Nahabedian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_nahabedian/32/16562_2.png) [@Mark\_Nahabedian](https://discourse.julialang.org/u/Mark_Nahabedian)\
**Post date:** [July 24, 2020, 2:33am UTC](https://discourse.julialang.org/t/can-i-get-the-declared-return-type-of-a-method/43575/14 "2020-07-24T02:33:06Z")

</div>

Thanks.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 24, 2020, 3:31am UTC](https://discourse.julialang.org/t/can-i-get-the-declared-return-type-of-a-method/43575/15 "2020-07-24T03:31:55Z")

</div>

I highly doubt this is the only / best approach to a constraint solver though. Maybe you should try something else.

---

<div class="post-metadata">

**Author:** ![Mark\_Nahabedian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_nahabedian/32/16562_2.png) [@Mark\_Nahabedian](https://discourse.julialang.org/u/Mark_Nahabedian)\
**Post date:** [July 24, 2020, 3:40am UTC](https://discourse.julialang.org/t/can-i-get-the-declared-return-type-of-a-method/43575/16 "2020-07-24T03:40:11Z")

</div>

Unfortunately, Core.Compiler.returb\_tyoe isn’t helpful. It I just returning Union{}, not sufficiently distinctive for my needs.

Also, though maybe I should open a new topic for this:

The signature of a method as appears in it’s sig field, is a Tuple type, e.g. Tuple{typeof(function name),Int32, Float64}. How can I determine the number of component types and get each one?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [July 24, 2020, 4:29am UTC](https://discourse.julialang.org/t/can-i-get-the-declared-return-type-of-a-method/43575/17 "2020-07-24T04:29:13Z")

</div>

If all the functions have the same input and output types, you may be looking for `FunctionWrappers.jl`.

How many different functions do you have? For example, can you just make a tuple of them as your global container once they are defined? It’s not clear what you mean by “discovering” them.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [July 24, 2020, 9:40am UTC](https://discourse.julialang.org/t/can-i-get-the-declared-return-type-of-a-method/43575/18 "2020-07-24T09:40:27Z")

</div>

Or even define them _in_ the tuple:

```julia
julia> const rules = (
           function rule1(x, y)::Int
             x + y
           end,
           function rule2(x, y)::Int
             x*y
           end
       );

julia> for rule in rules
         println(rule(1,2))
       end
3
2

```

---

<div class="post-metadata">

**Author:** ![haberdashPI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haberdashpi/32/26337_2.png) [@haberdashPI](https://discourse.julialang.org/u/haberdashPI)\
**Post date:** [July 24, 2020, 11:30am UTC](https://discourse.julialang.org/t/can-i-get-the-declared-return-type-of-a-method/43575/19 "2020-07-24T11:30:30Z")

</div>

Maybe one solution to your problem would be to use functors instead of functions. Then you can use introspection to find all subtypes of an abstract Rule type (using `subtypes`).

E.g.

```julia

abstract type Rule; end

struct Rule1 <: Rule; end
(r::Rule1)(x, y) = x + y

struct Rule2 <: Rule; end
(r::Rule2)(x,y) = x*y

x,y = 1,2
for R in subtypes(Rule)
   fn = R()
   println(fn(x,y))
end
```

---

<div class="post-metadata">

**Author:** ![Mark\_Nahabedian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_nahabedian/32/16562_2.png) [@Mark\_Nahabedian](https://discourse.julialang.org/u/Mark_Nahabedian)\
**Post date:** [July 24, 2020, 2:48pm UTC](https://discourse.julialang.org/t/can-i-get-the-declared-return-type-of-a-method/43575/20 "2020-07-24T14:48:59Z")

</div>

Thanks haberdashPI. I think that’s the cleanest, most useful suggestion I’ve had yet. I also like that I’d be reusing the same singletons.

In answer to pfitzseb, having a collection of my constraint functions is what I’m currently doing, but it is unsatisfactory because I expect it would impose definition order issues. Each constraint definition might be preferred by supporting functions and followed by a block of test code.

[Next page](https://discourse.julialang.org/t/can-i-get-the-declared-return-type-of-a-method/43575.md?page=2)
