# Convenience macro for type type-stable re-assignment / increment?

**URL:** <https://discourse.julialang.org/t/convenience-macro-for-type-type-stable-re-assignment-increment/10765>\
**Category:** General Usage\
**Created:** [May 7, 2018, 5:58pm UTC](https://discourse.julialang.org/t/convenience-macro-for-type-type-stable-re-assignment-increment/10765 "2018-05-07T17:58:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [May 7, 2018, 5:58pm UTC](https://discourse.julialang.org/t/convenience-macro-for-type-type-stable-re-assignment-increment/10765/1 "2018-05-07T17:58:50Z")

</div>

I find that expressions like

```
a = f(a, ...)
a += f(...)

```

can be a frequent source of nasty type instabilities. Nasty especially because they can lie dormant until generic code is suddenly used with a different numerical type, etc. So I typically  
guard against that with something like

```
a = oftype(a, f(a, ...)) 

```

But it’s tedious to write and makes the code harder to read. It might be nice to have a macro like

```
@typestable a = f(a, ...)
@typestable a += f(...)

```

that inserts the necessary `oftype` in assignment-like expressions. Do we have something like this around, already?

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [May 7, 2018, 6:40pm UTC](https://discourse.julialang.org/t/convenience-macro-for-type-type-stable-re-assignment-increment/10765/2 "2018-05-07T18:40:23Z")

</div>

Agree. Writing `n += UInt16(1)`, `n += int_T(1)`, `x *= Float32(2.0)` all over the place is very annoying. Unfortunately, I don’t know a less annoying way.

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [May 7, 2018, 6:53pm UTC](https://discourse.julialang.org/t/convenience-macro-for-type-type-stable-re-assignment-increment/10765/3 "2018-05-07T18:53:37Z")

</div>

If there’s no macro like this around yet, can someone suggest a fitting package to contribute this to? Or maybe we might even want something like this in Base?

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [May 7, 2018, 6:59pm UTC](https://discourse.julialang.org/t/convenience-macro-for-type-type-stable-re-assignment-increment/10765/4 "2018-05-07T18:59:13Z")

</div>

You should be able to do:

```julia
function f(...)
  A = typeof(a)
  a::A = f(a, ...)
  # now no more ::A is needed
  a = g(a, ...)
  ....
end

```

See [https://docs.julialang.org/en/stable/manual/types/#Type-Declarations-1](https://docs.julialang.org/en/stable/manual/types/#Type-Declarations-1)

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [May 7, 2018, 7:40pm UTC](https://discourse.julialang.org/t/convenience-macro-for-type-type-stable-re-assignment-increment/10765/5 "2018-05-07T19:40:15Z")

</div>

True - it doesn’t seem very common, though, for some reason (only a few instance in the whole of Base, also not frequent in common packages). But I agree, it’s probably the safest way. Is there some drawback to it? I just wonder why one doesn’t see it more often.

---

<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:** [May 8, 2018, 11:18am UTC](https://discourse.julialang.org/t/convenience-macro-for-type-type-stable-re-assignment-increment/10765/6 "2018-05-08T11:18:00Z")

</div>

> [@oschulz](#):
>
> Is there some drawback to it? I just wonder why one doesn’t see it more often.

I guess the intersection where the programmer can determine the type easily, but the compiler can’t, is pretty rare.

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [May 8, 2018, 12:37pm UTC](https://discourse.julialang.org/t/convenience-macro-for-type-type-stable-re-assignment-increment/10765/7 "2018-05-08T12:37:54Z")

</div>

> [@Tamas\_Papp](#):
>
> I guess the intersection where the programmer can determine the type easily, but the compiler can’t, is pretty rare.

I’m not quite sure I understand - whether I do

```julia
a = ...
...
a += typeof(a)(...)

```

or

```julia
x = ...
a::typeof(x) = x
...
a += ...

```

it’s type inference by the compiler in both cases, right?

The situation mostly occurs with variables that need to be modified within a loop (e.g. sums), of course.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [May 8, 2018, 1:23pm UTC](https://discourse.julialang.org/t/convenience-macro-for-type-type-stable-re-assignment-increment/10765/8 "2018-05-08T13:23:26Z")

</div>

Does this help?

```julia
 macro keeptype(x)
    y = x.args[1]
    z = x.args[2]
        f = z.args[1]
        a = z.args[2]
        b = z.args[3]
       return esc(:($y = $f($a, typeof($a)($b))))
end
@keeptype a = 1 + 2.0

```

This only works for two valued functions like `+`, but you could use splatting and mapping to make it more general.

---

<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:** [May 8, 2018, 4:29pm UTC](https://discourse.julialang.org/t/convenience-macro-for-type-type-stable-re-assignment-increment/10765/9 "2018-05-08T16:29:49Z")

</div>

In the second case, there is a type assertion too.

I was referring to the case where the type _is_ stable, it is just that the compiler can’t prove it. In my experience, this is getting quite rare in `v0.7`, but I tend to write short, small functions so it is possible that my style just meshes well with the compiler.
