Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: Why does this function return a value?

Tags:

julia

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?

like image 763
lispquestions Avatar asked Dec 11 '22 01:12

lispquestions


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.

like image 179
Fengyang Wang Avatar answered Dec 23 '22 19:12

Fengyang Wang


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.

like image 23
carstenbauer Avatar answered Dec 23 '22 21:12

carstenbauer