# How to treat one of the function arguments as a constant during broadcasting

**URL:** <https://discourse.julialang.org/t/how-to-treat-one-of-the-function-arguments-as-a-constant-during-broadcasting/65391>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [July 27, 2021, 5:00pm UTC](https://discourse.julialang.org/t/how-to-treat-one-of-the-function-arguments-as-a-constant-during-broadcasting/65391 "2021-07-27T17:00:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tonygem96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tonygem96/32/27564_2.png) [@tonygem96](https://discourse.julialang.org/u/tonygem96)\
**Post date:** [July 27, 2021, 5:00pm UTC](https://discourse.julialang.org/t/how-to-treat-one-of-the-function-arguments-as-a-constant-during-broadcasting/65391/1 "2021-07-27T17:00:05Z")

</div>

I wonder what is the correct way to create objects that are iterable (or broadcastable?). The code below works as expected.

```julia
struct A 
x::Float64
y::Float64
end 

function foo(n,m,s::A)
u = n^2 + s.x^2
v = m + s.y
return u,v
end

a = A(1,2)
u,v = foo(4,5,a)

```

However now I would like to be able to pass vectors to foo so it can also return vectors eg:

```julia
x = collect(1:10)
y = collect(2:11)
u,v = foo.(x,y,a)

```

Note here that ‘a’ is still a single object of type ‘A’. I have tried to extend length and iterate among others but to no avail. Thanks!

---

<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:** [July 27, 2021, 5:01pm UTC](https://discourse.julialang.org/t/how-to-treat-one-of-the-function-arguments-as-a-constant-during-broadcasting/65391/2 "2021-07-27T17:01:10Z")

</div>

> [@tonygem96](#):
>
> `u,v = foo.(x,y,a)`

`u,v = foo.(x,y,Ref(a))`

You’re asking how to NOT “expand” your struct during broad cast. You’re not iterating over your object

---

<div class="post-metadata">

**Author:** ![tonygem96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tonygem96/32/27564_2.png) [@tonygem96](https://discourse.julialang.org/u/tonygem96)\
**Post date:** [July 27, 2021, 5:04pm UTC](https://discourse.julialang.org/t/how-to-treat-one-of-the-function-arguments-as-a-constant-during-broadcasting/65391/3 "2021-07-27T17:04:51Z")

</div>

Precisely, thank you!

---

<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:** [July 27, 2021, 5:05pm UTC](https://discourse.julialang.org/t/how-to-treat-one-of-the-function-arguments-as-a-constant-during-broadcasting/65391/4 "2021-07-27T17:05:33Z")

</div>

maybe you want to revise your title a bit, but I’m not sure what’s the most likely titled to appear in google when someone searches this

---

<div class="post-metadata">

**Author:** ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)\
**Post date:** [July 27, 2021, 9:08pm UTC](https://discourse.julialang.org/t/how-to-treat-one-of-the-function-arguments-as-a-constant-during-broadcasting/65391/5 "2021-07-27T21:08:13Z")

</div>

> [@jling](#):
>
> u,v = foo.(x,y,Ref(a))

To make this work without the `Ref` (i.e., to always treat objects of type `A` as scalars when broadcasting), you can define

```julia
Base.Broadcast.broadcastable(a::A) = Ref(a)

```
