I specified a custom AbstractArray
type with additional generic parameters.
I wanted to make an alias for matrices and use an existing outer constructor,
but stumbled upon such a behaviour:
struct MyArray{T,N,K} <: AbstractArray{T,N}
t::T
end
MyMatrix{T} = MyArray{T,2,1}
MyArray{T,N,K}(x::AbstractArray{T,N}) where {T,N,K} = MyArray{T,N,K}(x[1])
mat = MyMatrix([1 2; 3 4])
MethodError: no method matching MyArray{T,2,1} where T(::Array{Int64,2})
To me, a constructor call with type MyArray{T,2,1} where T(::Array{Int64,2})
should be able to use
the one already declared.
I had a couple of other outer constructors, but all for the generic MyArray
(and apparently not any of the ones that would make the preceding code legal).
If I declare a specific constructor everything works:
MyMatrix(x::AbstractMatrix{T}) where {T} = MyMatrix{T}(x[1])
but it seems redundant to have constructors for aliases.
Similarly, I can specify the type with the constructor call:
mat = MyMatrix{Int}([1 2; 3 4])
but here the information is also repeated, as the type can be deduced from the argument.
Is there a way for me not to repeat the types or specialize the constructors when I use the matrix type?