-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Closed
Labels
breakingThis change will break codeThis change will break codedocsThis change adds or pertains to documentationThis change adds or pertains to documentation
Milestone
Description
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:2I expected this to work since the manual states:
defining
Base.convert(::Type{T}, args...) = ...automatically defines a constructorT(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.
Metadata
Metadata
Assignees
Labels
breakingThis change will break codeThis change will break codedocsThis change adds or pertains to documentationThis change adds or pertains to documentation