On Sat, Feb 20, 2016 at 12:58 PM, Samuel Powell <[email protected]> wrote:
> In 0.4.3, I have defined the following immutable, with a conversion rule to
> permit change the type of the wrapped tuple:
>
> immutable Foo{N,T}
>     co::NTuple{N,T}
> end
>
> convert{N,T}(::Type{Foo{N,T}}, input::Foo{N}) = Foo( ([T(input.co[i]) for i
> in input.co]...) )
>
> Bar = Foo((1.,2.)) # Returns Foo{2, Float64}((1.0,2.0))
>
> I can explicitly call the convert function:
>
> convert(Foo{2,Float32}, Bar)  Returns Foo{2,Float32}((1.0f0, 2.0f0))
>
> I expected that calling the constructor with the desired parameters would
> achieve the same, but in fact I receive an error:
>
> Foo{2,Float32}(Bar) # ERROR: MethodError: `convert` has no method matching
> convert(::Type{Tuple{Float32,Float32}}, ::Foo{2,Float64})
>
> I do not understand why Julia is dispatching to convert with an argument of
> ::Type{Tuple{Float32,Float32}}, instead of ::Type{Foo{2,Float32}}, can
> someone shed some light on what's going on?



See https://github.com/JuliaLang/julia/issues/15120

Defining convert does NOT define a constructor for you. In this case,
it perferred the default constructor of the `Foo` type. (See
`methods(Foo)`)

>
> Regards,
>
> Sam.
>

Reply via email to