I am having issues defining constructors for parametric types. I’lll borrow
an example from the docs to illustrate my issue:
Say I define this type:
type Point1{T}
x::T
y::T
end
I can then create an object of this type in many ways
julia> Point1(1.0, 2.0)
Point1{Float64}(1.0,2.0)
julia> Point1(1, 2)
Point1{Int64}(1,2)
julia> Point1("1", "2")
Point1{ASCIIString}("1","2")
julia> Point1('1', '2')
Point1{Char}('1','2')
julia> methods(Point1)
# 1 method for generic function "Point1":
Point1{T}(x::T,y::T)
# ect...
My question is how can I manually define the constructor for this
Parametric type?
To illustrate my issue, I tried to define a second type Point2 with the
same fields. After reading the documentation sections on types, methods,
and constructors I thought that I might be able to define the inner
constructor as Point2(x, y) = new(x, y) as follows:
type Point2{T}
x::T
y::T
Point2(x, y) = new(x, y)
end
However, this does not work:
julia> Point2(1.0, 2.0)
ERROR: no method Point2{T}(Float64, Float64)
julia> methods(Point2)
# 0 methods for generic function "Point2":
I thought maybe I needed to include the paramter T in the constructor, but
this doesn’t work either:
type Point2{T}
x::T
y::T
Point2{T}(x, y) = new(x, y)
end
It is odd, however, that if I pass in a DataType when calling my
Point2constructor it does work. For example:
julia> Point2{Float64}(1.0, 2.0)
Point2{Float64}(1.0,2.0)