# How do you address side effects for callable types?

**URL:** https://discourse.julialang.org/t/how-do-you-address-side-effects-for-callable-types/17072
**Category:** General Usage
**Created:** [November 1, 2018, 9:23pm UTC](https://discourse.julialang.org/t/how-do-you-address-side-effects-for-callable-types/17072 "2018-11-01T21:23:43Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)
#### Post date: [November 1, 2018, 9:23pm UTC](https://discourse.julialang.org/t/how-do-you-address-side-effects-for-callable-types/17072/1 "2018-11-01T21:23:44Z")

</div>

Say there is a callable type implemented with some side effect, but the variable name can be something without `!`, which looks like

```julia
count_foo = 0

struct Foo
    a::Int
end

(::Foo)(x::Int) = (global count_foo; count_foo += 1; count_foo)

foo = Foo(1.0)
foo(1) # this actually has some side effect

```

for functions, I can just name them with `!` as postfix, but what for callables? Is there any way to address this?

---

<div class="post-metadata">

### Author: ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)
#### Post date: [November 2, 2018, 1:43am UTC](https://discourse.julialang.org/t/how-do-you-address-side-effects-for-callable-types/17072/2 "2018-11-02T01:43:23Z")

</div>

There’s a few spots you can still put the !

```julia
struct Foo!;
  a::Int;
  end;

(::Foo!)(x) = ...

foo! = Foo!(1);
foo!(1)
```

---

<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: [November 2, 2018, 10:46am UTC](https://discourse.julialang.org/t/how-do-you-address-side-effects-for-callable-types/17072/3 "2018-11-02T10:46:02Z")

</div>

> [@Roger-luo](#):
>
> Is there any way to address this?

If you are asking about a style convention, I am not aware of any.

You could use variable names with `!` to emphasize that they change the argument (eg `foo! = `), but in your example `(::Foo)(::Int)` changes a global state and `!` is usually used for mutating the (first) argument. Perhaps use a different API, with a descriptive function name like `increment_global_count`.

---

<div class="post-metadata">

### Author: ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)
#### Post date: [November 3, 2018, 6:40pm UTC](https://discourse.julialang.org/t/how-do-you-address-side-effects-for-callable-types/17072/4 "2018-11-03T18:40:33Z")

</div>

Thanks. Err… maybe this is a bad example, e.g

```julia
struct MulByTwo
end

(::MulByTwo)(x::AbstractArray) = # this will multiply x with 2 and assign it back, e.g x *= 2

```

Then is it possible to (or force my users) mark their variable name with `!` or something?

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [November 3, 2018, 6:48pm UTC](https://discourse.julialang.org/t/how-do-you-address-side-effects-for-callable-types/17072/5 "2018-11-03T18:48:40Z")

</div>

> [@Roger-luo](#):
>
> Then is it possible to (or force my users) mark their variable name with `!` or something?

No. Not more than giving your singleton object a name you want by assigning it to a (constant) global variable. Note that such enforcement doesn’t even exist for “normal functions” either. Everyone is free to do `f = push!` and use the `f` that doesn’t have the `!` in the name.

---

<div class="post-metadata">

### Author: ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)
#### Post date: [November 3, 2018, 7:08pm UTC](https://discourse.julialang.org/t/how-do-you-address-side-effects-for-callable-types/17072/6 "2018-11-03T19:08:15Z")

</div>

I see. thanks! and just curious, will it be possible to ask the compiler warn people in the future when they try `f = push!`?

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [November 3, 2018, 7:30pm UTC](https://discourse.julialang.org/t/how-do-you-address-side-effects-for-callable-types/17072/7 "2018-11-03T19:30:13Z")

</div>

The compiler will almost certainly never warn that, there’s really nothing wrong with it. An external linter could.

Note that it can also appear when you pass function as an argument to another function and there’s little you can do about naming in that case.
