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?
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 isPoint{Float64}
. Thus, this method is only invoked when the first argument is the type valuePoint{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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With