# No method matching length() for any dot syntax operation involving a struct

**URL:** <https://discourse.julialang.org/t/no-method-matching-length-for-any-dot-syntax-operation-involving-a-struct/88425>\
**Category:** New to Julia\
**Created:** [October 8, 2022, 4:10am UTC](https://discourse.julialang.org/t/no-method-matching-length-for-any-dot-syntax-operation-involving-a-struct/88425 "2022-10-08T04:10:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ivoytov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ivoytov/32/42480_2.png) [@ivoytov](https://discourse.julialang.org/u/ivoytov)\
**Post date:** [October 8, 2022, 4:10am UTC](https://discourse.julialang.org/t/no-method-matching-length-for-any-dot-syntax-operation-involving-a-struct/88425/1 "2022-10-08T04:10:12Z")

</div>

I have experienced this problem a lot and it’s beyond frustrating. For some reason any dot syntax call that includes a `struct` fails. I can use `map` but is it possible to use dot syntax here? What am I missing?

```julia
julia> struct MyType
       name::String
       end

julia> f(m::MyType, i::Integer) = i + 1
f (generic function with 1 method)

julia> arr = [1,2,3]
3-element Vector{Int64}:
 1
 2
 3

julia> my = MyType("John")
MyType("John")

julia> f.(my, arr)
ERROR: MethodError: no method matching length(::MyType)
[...]

julia> map(x-> f(my, x), arr)
3-element Vector{Int64}:
 2
 3
 4

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 8, 2022, 4:18am UTC](https://discourse.julialang.org/t/no-method-matching-length-for-any-dot-syntax-operation-involving-a-struct/88425/2 "2022-10-08T04:18:47Z")

</div>

broadcasting treats things as vectors by default. If you want broadcasting to treat `MyType` as a scalar, define `Base.broadcastable(x::MyType) = Ref(x)`

---

<div class="post-metadata">

**Author:** ![ivoytov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ivoytov/32/42480_2.png) [@ivoytov](https://discourse.julialang.org/u/ivoytov)\
**Post date:** [October 8, 2022, 4:25am UTC](https://discourse.julialang.org/t/no-method-matching-length-for-any-dot-syntax-operation-involving-a-struct/88425/3 "2022-10-08T04:25:32Z")

</div>

Wow, thanks - what a great trick. Is there a reason not to use this line in every single type definition? Would I be breaking anything by overriding `broadcastable` like this?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 8, 2022, 4:48am UTC](https://discourse.julialang.org/t/no-method-matching-length-for-any-dot-syntax-operation-involving-a-struct/88425/4 "2022-10-08T04:48:22Z")

</div>

> [@ivoytov](#):
>
> Would I be breaking anything by overriding `broadcastable` like this?

no, this is what you should do, broadcast is an interface:  
[https://docs.julialang.org/en/v1/manual/interfaces/#man-interfaces-broadcasting](https://docs.julialang.org/en/v1/manual/interfaces/#man-interfaces-broadcasting)

this is not overriding, nor hack, because you own the type, and you need to teach Julia what to do with your types during broadcasting. Another similar concept is number type promotion

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [October 8, 2022, 4:54am UTC](https://discourse.julialang.org/t/no-method-matching-length-for-any-dot-syntax-operation-involving-a-struct/88425/5 "2022-10-08T04:54:51Z")

</div>

> [@Oscar\_Smith](#):
>
> If you want broadcasting to treat `MyType` as a scalar, define `Base.broadcastable(x::MyType) = Ref(x)`

Neat!

> [@ivoytov](#):
>
> Is there a reason not to use this line in every single type definition?

Could be made default with `Base.broadcastable(x::Any) = Ref(x)`, and only overridden explicitly for objects that can be broadcast such as tuples, arrays and matrices. Why not do things this way? 🤔

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 8, 2022, 5:00am UTC](https://discourse.julialang.org/t/no-method-matching-length-for-any-dot-syntax-operation-involving-a-struct/88425/6 "2022-10-08T05:00:05Z")

</div>

So that is how it works for `Number`s. The reason for the decision is that there were deemed to be more collection-like objects rather than scalar-like non-number objects. This is a very subjective decision obviously, and either way, someone needs to change the behavior for their type. Note that if you are defining a lot of similar scalar-like types, you can define `broadcastable` on a common super-type to give all subtypes that behavior.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [October 8, 2022, 5:27am UTC](https://discourse.julialang.org/t/no-method-matching-length-for-any-dot-syntax-operation-involving-a-struct/88425/7 "2022-10-08T05:27:55Z")

</div>

People wanted to remove this behavior from numbers [make numbers non-iterable? · Issue #7903 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/7903#issuecomment-217219942) but it was too much work at the time, and now it’s too late. I wouldn’t suggest adding collection semantics to other arbitrary types that aren’t actually collections.
