# Struct and vectorised function

**URL:** <https://discourse.julialang.org/t/struct-and-vectorised-function/84159>\
**Category:** General Usage\
**Tags:** struct, functions\
**Created:** [July 13, 2022, 11:01am UTC](https://discourse.julialang.org/t/struct-and-vectorised-function/84159 "2022-07-13T11:01:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [July 13, 2022, 11:01am UTC](https://discourse.julialang.org/t/struct-and-vectorised-function/84159/1 "2022-07-13T11:01:05Z")

</div>

```julia
struct X; x end 
struct Y; y end

f(::X,::Y) = 1

f.( [X(1),X(1)], Y(1) )

```

This gives an error no method matching length(::Y)

Is there a way to tell it that the “.” vectorisation of f() is over the vector of X, not over Y ?

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [July 13, 2022, 11:10am UTC](https://discourse.julialang.org/t/struct-and-vectorised-function/84159/2 "2022-07-13T11:10:19Z")

</div>

You can package `Y(1)` into a one element container. For `Ref` or `tuple` are common choices:

```julia
f.( [X(1),X(1)], Ref(Y(1)) )
f.( [X(1),X(1)], (Y(1),) )

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [July 13, 2022, 12:51pm UTC](https://discourse.julialang.org/t/struct-and-vectorised-function/84159/3 "2022-07-13T12:51:21Z")

</div>

For your own structs, you can also define `Base.Broadcast.broadcastable` to tell Julia to treat it as a scalar for broadcasting:

```julia
julia> Base.Broadcast.broadcastable(y::Y) = Ref(y)

julia> f(::X,::Y) = 1
f (generic function with 1 method)

julia> f.( [X(1),X(1)], Y(1) )
2-element Vector{Int64}:
 1
 1

```
