# Return Type Declarations what are the semantics of f(x)::Any?

**URL:** <https://discourse.julialang.org/t/return-type-declarations-what-are-the-semantics-of-f-x-any/4575>\
**Category:** Internals & Design\
**Tags:** type\
**Created:** [June 30, 2017, 8:30pm UTC](https://discourse.julialang.org/t/return-type-declarations-what-are-the-semantics-of-f-x-any/4575 "2017-06-30T20:30:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpfairbanks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpfairbanks/32/4500_2.png) [@jpfairbanks](https://discourse.julialang.org/u/jpfairbanks)\
**Post date:** [June 30, 2017, 8:30pm UTC](https://discourse.julialang.org/t/return-type-declarations-what-are-the-semantics-of-f-x-any/4575/1 "2017-06-30T20:30:27Z")

</div>

From this section of the manual, there is no mention of what happens with return type ::Any [https://docs.julialang.org/en/stable/manual/types/#Type-Declarations-1](https://docs.julialang.org/en/stable/manual/types/#Type-Declarations-1)

This came up in [https://github.com/simonster/Memoize.jl/pull/18](https://github.com/simonster/Memoize.jl/pull/18), what is the correct semantics of functions that declare a return type of Any? Are they exactly the same as not declaring a return type? Since whatever type is returned satisfies T\<:Any, I would expect them to have the same meaning. The method definition expression is different as a macro that takes a function definition can see the AST node with `ex.args[1].head == :(::)` in the case of return types and no such node when there is no return type declared.

Would it be safe to add a return type declaration of Any to all functions that do not declare an return type?

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [June 30, 2017, 8:57pm UTC](https://discourse.julialang.org/t/return-type-declarations-what-are-the-semantics-of-f-x-any/4575/2 "2017-06-30T20:57:26Z")

</div>

Return type annotation means any value returned will be converted to that type (or an error is thrown). `convert(Any, myvar)` is a no-op as it dispatches to `convert{T}(::Type{T}, x::T) = x`. Thus, indeed, return type `Any` is identical to not declaring one.

---

<div class="post-metadata">

**Author:** ![jpfairbanks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpfairbanks/32/4500_2.png) [@jpfairbanks](https://discourse.julialang.org/u/jpfairbanks)\
**Post date:** [July 1, 2017, 6:13pm UTC](https://discourse.julialang.org/t/return-type-declarations-what-are-the-semantics-of-f-x-any/4575/3 "2017-07-01T18:13:27Z")

</div>

Thanks @mauro3. This resolves my question for Memoize. Memoized functions will default to a return type declaration of Any even if the original function has no return type annotation.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [July 1, 2017, 11:59pm UTC](https://discourse.julialang.org/t/return-type-declarations-what-are-the-semantics-of-f-x-any/4575/4 "2017-07-01T23:59:07Z")

</div>

FWIW, there’s a few wrong information in this comment…

> [@mauro3](#):
>
> convert(Any, myvar) is a no-op

This is mostly true, though there’s nothing that stops the user from overwriting it (they shouldn’t but still).

> [@mauro3](#):
>
> it dispatches to convert{T}(::Type{T}, x::T) = x

No, it dispatches to `convert(::Type{Any}, x::ANY)`.

> [@mauro3](#):
>
> Thus, indeed, return type Any is identical to not declaring one.

The behavior is ideally the same if no one have defined. It can in principle also cause performance decrease though the inference seems to be ok at this.
