Can somebody explain this syntax?

We know that Typenames can be used as constructors
for example


I wanted to check how this is possible, so I ran

@which Float64(1.0)

It printed the following

(::Type{T})(x::T) where {T<:Number} = x

What is this mysterious syntax? The most mysterious part is the (::Type{T}).
Where is the function name? I do know about the Type{T} objects,
but I can’t get my head around the syntax. Plz help

1 Like

The syntax (::X)(...) is for making objects of type X callable. Here’s an example. By default strings are not callable:

julia> "a"("b")
ERROR: MethodError: objects of type String are not callable
 [1] top-level scope
   @ REPL[1]:1

Now we use the syntax from above and make all strings callable, executing a concatenation function (this is of course a silly example and there’s no reason why you’d do this)

julia> (s::String)(x) = s * x

julia> "a"("b")

So the syntax you asked about is a generic definition which just says that calling any Number subtype with a number of that type returns the number. So Float64(1.0) = 1.0. I don’t know why that needs to exist specifically, but that’s what the code does.


that’s amazing, thanks for the clear reply. Would be good to have this in the docs

1 Like

In the julia docs there is a section that talks about this

Methods are associated with types, so it is possible to make any arbitrary Julia object “callable” by adding methods to its type. (Such “callable” objects are sometimes called “functors.”)

Methods · The Julia Language