Which methods must be implemented for a custom type to support broadcasting

broadcast

#1

Hey,

I wanted to leverage the new “.” notation for broadcasting for methods of a custom type, which methods does the type need in order to support automatic broadcasting?

say

type A
    val::Float64
end


function f(a::A, x::Float64)::Float64 x end

a = A(1.0)
f.(a, [1.0; 2.0])

This initially throws a error about “size” not being implemented and function size(x::A) size(1) end resolves this,
but the next issue is getindex… Is there like a list of methods I need to get “.” running with methods for “A”?


#2

That example should work on the master branch without defining any additional method. On 0.5, for a custom type, you need size and getindex. For your example above, just define

Base.size(::A) = ()
Base.getindex(a::A, i) = a

#3

Thanks a lot, easier than I feared ;). For all those looking for a quick-and-dirty fix: It also works if you simply call f.([a], [1…0; 2.0]), i.e. by creating an single item array on the fly.


#4

More generally, the methods you need to implement to support a given interface are presented in the manual.