У меня есть два типа A
и B
.Я написал B(a::A)
и convert(::Type{B}, a)
, но это не сразу заставляет Array{B}(as::Array{A})
работать.Должен ли я написать этот метод, а?Согласно документации , я ожидаю, что Джулия позаботится обо мне.
struct A end
struct B end
B(a::A) = B();
convert(::Type{B}, a) = B(a);
# These work
B(A());
convert(B, A());
# This doesn't
Array{B}([A()]);
Это ошибка
ERROR: LoadError: MethodError: Cannot `convert` an object of type A to an object of type B
Closest candidates are:
convert(::Type{T}, !Matched::T) where T at essentials.jl:154
B(::A) at /home/mvarble/test.jl:3
Stacktrace:
[1] setindex!(::Array{B,1}, ::A, ::Int64) at ./array.jl:767
[2] copyto! at ./abstractarray.jl:753 [inlined]
[3] copyto! at ./abstractarray.jl:745 [inlined]
[4] Type at ./array.jl:482 [inlined]
[5] Array{B,N} where N(::Array{A,1}) at ./boot.jl:427
[6] top-level scope at none:0
[7] include at ./boot.jl:326 [inlined]
[8] include_relative(::Module, ::String) at ./loading.jl:1038
[9] include(::Module, ::String) at ./sysimg.jl:29
[10] exec_options(::Base.JLOptions) at ./client.jl:267
[11] _start() at ./client.jl:436