# Broadcasting not on all arguments

**URL:** <https://discourse.julialang.org/t/broadcasting-not-on-all-arguments/44027>\
**Category:** General Usage\
**Created:** [July 31, 2020, 9:57am UTC](https://discourse.julialang.org/t/broadcasting-not-on-all-arguments/44027 "2020-07-31T09:57:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![drhouse82](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drhouse82/32/9806_2.png) [@drhouse82](https://discourse.julialang.org/u/drhouse82)\
**Post date:** [July 31, 2020, 9:57am UTC](https://discourse.julialang.org/t/broadcasting-not-on-all-arguments/44027/1 "2020-07-31T09:57:42Z")

</div>

I find myself thinking in common OOP terms and trying to have object representing a model that has some method to do something to outside data.

So I thought, in Julia, the model should likely be a struct and the method should accept the struct as an argument.

The simplified example I can come up with:

```julia
mutable struct CarModel
    # some car properties
    consumption
end

function predict_consumption(carmodel, distance)
    return carmodel.consumption*distance
end

myCar = CarModel(10)

consumption_values = predict_consumption(myCar, 10)

```

However, when doing so, I cannot use dot-broadcasting on the input data

```julia
distances = 0:100
consumption_values = predict_consumption.(myCar, distances)

ERROR: LoadError: MethodError: no method matching length(::CarModel)

```

I can fully imagine, that this is the most sensible behavior, since Julia cannot find out if myCar is to be broadcasted on or if it is a scalar.

Is there some elegant or commonly used approach for such a scenario of mimicking mehtods inside objects by functions taking structs + actual parameters?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 31, 2020, 10:06am UTC](https://discourse.julialang.org/t/broadcasting-not-on-all-arguments/44027/2 "2020-07-31T10:06:38Z")

</div>

Either you want

```julia
predict_consumption.(Ref(myCar), distances)

```

or define something like

```julia
Base.broadcastable(x::CarModel) = Ref(x)

```

to do this automatically for your type. See the manual on broadcasting.
