What properties must a custom type have, to work with the new vectorised syntax?



I was reading up on the new vectorized function syntax, and I wanted to try it out with BioSequence from the package Bio.jl and a toy example (below). BioSequences behave like a vector/array{1} of various subtypes of Nucleotide, but they are not AbstractArrays or anything like that.

I was wondering if there were a number of methods or characteristics a user defined type must have in order to work with the new vectorised/broadcast function syntax.

julia> using Bio.Seq

julia> ismismatch(a::Nucleotide, b::Nucleotide) = a != b
ismismatch (generic function with 1 method)

julia> as = dna"AAAAAAAAAA"
10nt DNA Sequence:

julia> bs = dna"AAAAAATTTT"
10nt DNA Sequence:

julia> ismismatch.(as, bs)
ERROR: MethodError: no method matching getindex(::Bio.Seq.BioSequence{Bio.Seq.DNAAlphabet{4}}, ::CartesianIndex{1})
 in macro expansion at ./broadcast.jl:127 [inlined]
 in macro expansion at ./simdloop.jl:73 [inlined]
 in macro expansion at ./broadcast.jl:123 [inlined]
 in _broadcast!(::#ismismatch, ::Array{Bool,1}, ::Tuple{Tuple{Bool},Tuple{Bool}}, ::Tuple{Tuple{Int64},Tuple{Int64}}, ::Tuple{Bio.Seq.BioSequence{Bio.Seq.DNAAlphabet{4}},Bio.Seq.BioSequence{Bio.Seq.DNAAlphabet{4}}}, ::Type{Val{2}}) at ./broadcast.jl:117
 in broadcast!(::Function, ::Array{Bool,1}, ::Bio.Seq.BioSequence{Bio.Seq.DNAAlphabet{4}}, ::Bio.Seq.BioSequence{Bio.Seq.DNAAlphabet{4}}) at ./broadcast.jl:172
 in broadcast_t(::Function, ::Type{T}, ::Bio.Seq.BioSequence{Bio.Seq.DNAAlphabet{4}}, ::Vararg{Bio.Seq.BioSequence{Bio.Seq.DNAAlphabet{4}},N}) at ./broadcast.jl:228
 in broadcast(::Function, ::Bio.Seq.BioSequence{Bio.Seq.DNAAlphabet{4}}, ::Bio.Seq.BioSequence{Bio.Seq.DNAAlphabet{4}}) at ./broadcast.jl:230


None on 0.6. Hard/impossible to fix on 0.5.


Actually I assumed that you want to use them as scalar, which should be the default now on 0.6.

If you want them to behave like arrays, ref https://github.com/JuliaLang/julia/issues/20740, though I’ve only seen extending it for AbstractArrays.