When I run the following code
type A
v::Float64
end
type B
v::Float64
end
Base.convert(::Type{A}, b::B) = A(b.v/2)
b = B(2)
A(b)
it fails with this error message because the implicit constructor is called.
ERROR: MethodError: `convert` has no method matching convert(::Type{Float64}, ::A)
This may have arisen from a call to the constructor Float64(...),
since type constructors fall back to convert methods.
Closest candidates are:
call{T}(::Type{T}, ::Any)
convert(::Type{Float64}, ::Int8)
convert(::Type{Float64}, ::Int16)
...
in call at none:2
I expected this to work since the manual states:
defining Base.convert(::Type{T}, args...) = ... automatically defines a constructor T(args...) = ...."
The following code on the other hand works:
type A
v::Float64
end
Base.convert(::Type{Float64}, a::A) = a.v/12
a = A(2)
Float64(a)
It seems that the new constructor is only generated for built-in types and not for custom types.
EDIT: Typo.
When I run the following code
type A v::Float64 end type B v::Float64 end Base.convert(::Type{A}, b::B) = A(b.v/2) b = B(2) A(b)it fails with this error message because the implicit constructor is called.
I expected this to work since the manual states:
The following code on the other hand works:
type A v::Float64 end Base.convert(::Type{Float64}, a::A) = a.v/12 a = A(2) Float64(a)It seems that the new constructor is only generated for built-in types and not for custom types.
EDIT: Typo.