I encountored syntax like funcion (::Type{T})(...)...
and it seems that (::Type{T})
is a function name. Is it correct? Also, if so, could you explain how it works or tell me the corresponding documentation?
I encountored this syntax here. For example, the code below can be found at the lines 82-87.
function (::Type{M})(
vertices::AbstractVector{Point{3, VT}}, faces::AbstractVector{FT}
) where {M <: HMesh, VT, FT <: Face}
msh = PlainMesh{VT, FT}(vertices = vertices, faces = faces)
convert(M, msh)
end
See https://docs.julialang.org/en/latest/manual/methods/#Function-like-objects-1
(and maybe this if you’re unfamiliar with Type{T}
)
The snippet you posted basically defines constructors - it is defining a method for DataType
objects which are a subtype of M
. However, it doesn’t have to be a constructor in the sense of creating an object:
abstract type M end
struct A <: M end
struct B <: M end
function (::Type{T})(s::String) where {T <: M}
println("I'm not constructing an object of type ", T, ". Instead I just print: ", s)
end
which gives
julia> A("test")
I'm not constructing an object of type A. Instead I just print: test
julia> B("hello world")
I'm not constructing an object of type B. Instead I just print: hello world
6 Likes