# Assignment in function arguments

**URL:** <https://discourse.julialang.org/t/assignment-in-function-arguments/12665>\
**Category:** General Usage\
**Created:** [July 26, 2018, 11:59am UTC](https://discourse.julialang.org/t/assignment-in-function-arguments/12665 "2018-07-26T11:59:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlhetland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlhetland/32/283_2.png) [@mlhetland](https://discourse.julialang.org/u/mlhetland)\
**Post date:** [July 26, 2018, 11:59am UTC](https://discourse.julialang.org/t/assignment-in-function-arguments/12665/1 "2018-07-26T11:59:58Z")

</div>

One of the references I found for this were in an old discussion about keyword arguments, where it seems the conclusion was that [they prevented the possibility of assigments](https://github.com/JuliaLang/julia/issues/485#issuecomment-15221092) in function arguments. That makes sense for assignments that are indistinguishable from keyword arguments, but shouldn’t it be okay otherwise? E.g., if one wraps the assignment in parentheses? (Python, for one, makes this distinction – `f(x=1)` is fine, but `f((x=1))` is a syntax error, and thus this syntax would have been free.)

My use-case is in a loop where I keep calling a function until (yes, I could use other iteration tools for this ;-)) such as

```julia
while (x = func(y)) ≢ nothing
    …
end

```

This, of course, works fine. However, if I need a function rather than an operator, it doesn’t work:

```julia
while !isa((x = func(y)), Z)
    …
end

```

Here Julia thinks I’m trying to use a keyword argument `x`. Is it necessary to permit calling keyword arguments in parentheses like this? Wouldn’t it be more useful to permit assignments? Or is there perhaps some straightforward workaround I’m missing (other than defining an operator for the function, which is an option in the binary case)?

---

<div class="post-metadata">

**Author:** ![mlhetland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlhetland/32/283_2.png) [@mlhetland](https://discourse.julialang.org/u/mlhetland)\
**Post date:** [July 26, 2018, 12:01pm UTC](https://discourse.julialang.org/t/assignment-in-function-arguments/12665/2 "2018-07-26T12:01:37Z")

</div>

Okay, `isa` was a bad example, as it’s already a binary operator xD But you get the idea!

---

<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 26, 2018, 12:05pm UTC](https://discourse.julialang.org/t/assignment-in-function-arguments/12665/3 "2018-07-26T12:05:09Z")

</div>

This is very subjective, but I think this leads to code that is difficult to read. Sometimes I also do similar things, but I regret it later most of the time.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [July 26, 2018, 12:11pm UTC](https://discourse.julialang.org/t/assignment-in-function-arguments/12665/4 "2018-07-26T12:11:42Z")

</div>

Adding a semi-colon does what you want.

```julia
julia> isa((x = 1;), Int)
true

julia> x
1

```

---

<div class="post-metadata">

**Author:** ![mlhetland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlhetland/32/283_2.png) [@mlhetland](https://discourse.julialang.org/u/mlhetland)\
**Post date:** [July 26, 2018, 12:17pm UTC](https://discourse.julialang.org/t/assignment-in-function-arguments/12665/5 "2018-07-26T12:17:28Z")

</div>

Indeed it is subjective. The vitriol over the introduction of assignment expressions is (as I understand it) what drove Guido van Rossum to step back as figurehead for Python a couple of weeks ago. So some people really hate them, I guess.

---

<div class="post-metadata">

**Author:** ![mlhetland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlhetland/32/283_2.png) [@mlhetland](https://discourse.julialang.org/u/mlhetland)\
**Post date:** [July 26, 2018, 1:53pm UTC](https://discourse.julialang.org/t/assignment-in-function-arguments/12665/6 "2018-07-26T13:53:02Z")

</div>

One alternative would be to use somthing like Python’s `iter(callable, sentinel)` (which I can’t find in the standard library or in `IterTools.jl`), though that would require introducing `() -> func(y)`, and would only work for equality. And a more specialized solution (e.g., including the argument to the callable, and having a predicate rather than a sentinel value) would perhaps be better handled by individual projects. (Though Python at least has `takewhile`, which is related. Maybe worth a PR.)

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [July 26, 2018, 11:23pm UTC](https://discourse.julialang.org/t/assignment-in-function-arguments/12665/7 "2018-07-26T23:23:00Z")

</div>

You can also just do:

```julia
x = func(y)
while !isa(x, Z)
    ....
    x = func(y)
end

```

---

<div class="post-metadata">

**Author:** ![mlhetland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlhetland/32/283_2.png) [@mlhetland](https://discourse.julialang.org/u/mlhetland)\
**Post date:** [July 26, 2018, 11:26pm UTC](https://discourse.julialang.org/t/assignment-in-function-arguments/12665/8 "2018-07-26T23:26:38Z")

</div>

Sure. The motivation was trying to avoid the redundancy. (A common Python pattern is to use an infinite loop with a conditional break – which sort of seems to indicate a missing looping construct. Or, indeed, assignment expressions, which have now been introduced.)
