Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia what does a nameless value in the function header mean?

Tags:

julia

I often see something like the following in Julia:

convert(::Type{Point{Float64}}, ::Float64)

how does the (:: work? And what is the terminology for this?

like image 757
tlnagy Avatar asked May 04 '16 22:05

tlnagy


1 Answers

Your answer can be found in the Julia documentation for defining conversions. To quote (with types switched to make it even more straightforward to read):

The type of the first argument of this method is a singleton type, Type{Point{Float64}}, the only instance of which is Point{Float64}. Thus, this method is only invoked when the first argument is the type value Point{Float64}. Notice the syntax used for the first argument: the argument name is omitted prior to the :: symbol, and only the type is given. This is the syntax in Julia for a function argument whose type is specified but whose value is never used in the function body. In this example, since the type is a singleton, there would never be any reason to use its value within the body.

(Emphasis mine)

You will also encounter the foo(::SomeType) syntax in error messages, when trying to invoke a function with arguments of the wrong type (after all you can't show the argument names of a variant that does not exist). E.g:

julia> foo(x::Bool) = 3
foo (generic function with 1 method)

julia> foo(5)
ERROR: `foo` has no method matching foo(::Int64)
like image 123
Erik Vesteraas Avatar answered Oct 09 '22 20:10

Erik Vesteraas