Julia: Why does this function return a value?

Viewed 136

New to Julia, walking through the manual and got to this example under Methods:

julia> mytypeof(x::T) where {T} = T
mytypeof (generic function with 1 method)

When you call this method with values for x, it gives you back the type:

julia> mytypeof(1)
Int64

julia> mytypeof(1.0)
Float64

My question is: why does this return a value at all? Where, in other words, is the implied return value?

2 Answers

You should read

mytypeof(x::T) where {T} = T

As

(mytypeof(x::T) where {T}) = T

That is, the = T is not part of the where clause; it's the RHS of the function.

In Julia, by default, the last value of a function body is automatically returned.

In your case the function body is just T. Hence, T gets returned. (Think of it actually being return T.)

If you don't want to return anything you can return nothing.

Related