lispquestions
lispquestions

Reputation: 441

Julia: Why does this function return a value?

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?

Upvotes: 4

Views: 200

Answers (2)

carstenbauer
carstenbauer

Reputation: 10117

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.

Upvotes: 5

Fengyang Wang
Fengyang Wang

Reputation: 12051

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.

Upvotes: 8

Related Questions