# Proposal: broadcast operator for primitive types

**URL:** <https://discourse.julialang.org/t/proposal-broadcast-operator-for-primitive-types/24917>\
**Category:** General Usage\
**Created:** [June 4, 2019, 2:00pm UTC](https://discourse.julialang.org/t/proposal-broadcast-operator-for-primitive-types/24917 "2019-06-04T14:00:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [June 4, 2019, 2:00pm UTC](https://discourse.julialang.org/t/proposal-broadcast-operator-for-primitive-types/24917/1 "2019-06-04T14:00:08Z")

</div>

Hi!

I was coding something today, and I have a variable `V` which can be of many types (`Int`, `Float32`, `Float64`, and so on). However, I need to limit the value of this variable. What I did was:

```julia
V > +10 && (V = eltype(V)(+10))
V < -10 && (V = eltype(V)(-10))

```

However I found this strange… So, I would like to propose the indrocution of broadcast operator for such primitive types, so that we can do:

```julia
V .= 10

```

and V will not change type. Of course, if conversion is not possible, then an error will be thrown.

What do you think?

**EDIT** : I know we have the `sign` function that would do what I need, but I still think this will be nice to have.

---

<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:** [June 4, 2019, 2:14pm UTC](https://discourse.julialang.org/t/proposal-broadcast-operator-for-primitive-types/24917/2 "2019-06-04T14:14:04Z")

</div>

I am confused, where does broadcasting happen here? `V` looks like a scalar.

Also, why the `eltype`? `V = oftype(V, 10)` would be simpler.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [June 4, 2019, 2:22pm UTC](https://discourse.julialang.org/t/proposal-broadcast-operator-for-primitive-types/24917/3 "2019-06-04T14:22:23Z")

</div>

> [@Tamas\_Papp](#):
>
> I am confused, where does broadcasting happen here? `V` looks like a scalar.

It is not “broadcast”, I do not know how to call it… but an operator that will keep the type of the variable on the left side if it is a scalar. Doesn’t make sense?

> [@Tamas\_Papp](#):
>
> Also, why the `eltype` ? `V = oftype(V, 10)` would be simpler.

Yes, that will be much better than my previous approach.

---

<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:** [June 4, 2019, 2:33pm UTC](https://discourse.julialang.org/t/proposal-broadcast-operator-for-primitive-types/24917/4 "2019-06-04T14:33:35Z")

</div>

> [@Ronis\_BR](#):
>
> an operator that will keep the type of the variable on the left side

I can see a use case for this (enforcing type stability in certain situations), but would not conflate it with broadcasting.

Possibly a macro that expands

```julia
@keeptype v = rhs

```

to

```julia
v = oftype(v, rhs)

```

could be useful. OTOH `oftype` is shorter and already exists.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [June 4, 2019, 2:41pm UTC](https://discourse.julialang.org/t/proposal-broadcast-operator-for-primitive-types/24917/5 "2019-06-04T14:41:20Z")

</div>

The problem IMHO is for bigger expressions, like:

```julia
v = oftype(v, theta + omega + alpha/2 + gamma - beta - 180)

```

but a macro will be ok too 🙂

The suggestion of `.=` was to give the meaning that we are “modifying” the value of a variable but keeping the type.

---

<div class="post-metadata">

**Author:** ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)\
**Post date:** [June 4, 2019, 3:21pm UTC](https://discourse.julialang.org/t/proposal-broadcast-operator-for-primitive-types/24917/6 "2019-06-04T15:21:15Z")

</div>

Can’t you do this?

```julia
V::T = 10

```

where `T` is the type you want.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [June 4, 2019, 3:56pm UTC](https://discourse.julialang.org/t/proposal-broadcast-operator-for-primitive-types/24917/7 "2019-06-04T15:56:40Z")

</div>

> [@Ronis\_BR](#):
>
> The suggestion of `.=` was to give the meaning that we are “modifying” the value of a variable but keeping the type.

This is a little pedantic, but Julia’s identifiers aren’t really “variables.” They’re just names. So the only way to “modify” the name is to completely re-purpose the same name to identify something different.

Using `.=` and thinking about “modifying” a variable leads your mental model down a path that points towards more confusion, I think.

What you can do is declare that this name will always identify something with a certain type with the `V::T = ...` syntax. You could do this as a macro, too, with a `@typeconst V = rhs` expanding to `V::typeof(rhs) = rhs`.

But to your actual original example: just use `clamp` 🙂

```julia
julia> V = 8.0
8.0

julia> clamp(V, -10, 10)
8.0

julia> V = 22.5
22.5

julia> clamp(V, -10, 10)
10.0

```

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [June 4, 2019, 7:43pm UTC](https://discourse.julialang.org/t/proposal-broadcast-operator-for-primitive-types/24917/8 "2019-06-04T19:43:56Z")

</div>

> [@favba](#):
>
> Can’t you do this?

I would need to compute `T`, which in that case I did not have. So there would be a `T = eltype(V)` somewhere.

> [@mbauman](#):
>
> This is a little pedantic, but Julia’s identifiers aren’t really “variables.” They’re just names. So the only way to “modify” the name is to completely re-purpose the same name to identify something different.
> 
> Using `.=` and thinking about “modifying” a variable leads your mental model down a path that points towards more confusion, I think.

Fair enough!

> [@mbauman](#):
>
> What you can do is declare that this name will always identify something with a certain type with the `V::T = ...` syntax. You could do this as a macro, too, with a `@typeconst V = rhs` expanding to `V::typeof(rhs) = rhs` .

I really liked this idea 🙂 I will try to put in a package.

> [@mbauman](#):
>
> But to your actual original example: just use `clamp` 🙂

Indeed! @mcabbott told me about this function in Slack. I did not know how I missed that. You have no idea how many times I needed to do such a thing 😄 living and learning!

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [June 5, 2019, 6:36am UTC](https://discourse.julialang.org/t/proposal-broadcast-operator-for-primitive-types/24917/9 "2019-06-05T06:36:14Z")

</div>

> [@mbauman](#):
>
> clamp

Great function and I love the choice of verb…Very easy to remember! 🙂
