How do I tell Julia that my object is not iterable? How do I make a, b, c = some_function.(mytype, (1, 2, 3))
work automatically without throwing no method matching length(::MyType)
?
1 Like
the easy way:
wrap it in something iterable
some_function.(Ref(mytype), (1, 2, 3))
if you want this to work in general without wrapping would be to change the BroadcastStyle
of your object
1 Like
You want Base.broadcastable(x::T) = Ref(x)
. See here
julia> struct A end
julia> foo(a::A, x) = x;
julia> foo.(A(), [1, 2])
ERROR: MethodError: no method matching length(::A)
julia> Base.broadcastable(a::A) = Ref(a);
julia> foo.(A(), [1, 2])
2-element Vector{Int64}:
1
2
12 Likes