# Syntactic sugar: (x==2) and y=12 ; or even (x == 2) && y=12

**URL:** <https://discourse.julialang.org/t/syntactic-sugar-x-2-and-y-12-or-even-x-2-y-12/10969>\
**Category:** Internals & Design\
**Created:** [May 17, 2018, 10:10pm UTC](https://discourse.julialang.org/t/syntactic-sugar-x-2-and-y-12-or-even-x-2-y-12/10969 "2018-05-17T22:10:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [May 17, 2018, 10:10pm UTC](https://discourse.julialang.org/t/syntactic-sugar-x-2-and-y-12-or-even-x-2-y-12/10969/1 "2018-05-17T22:10:37Z")

</div>

can you tell that I used perl a lot 😉 .

could

```julia
julia> (x == 2) && y=12

```

clash with anything? julia does not have the less binding “and” and “or” keywords afaik. I would love them, too.

I think the user meaning for the ‘&&’ construct is amply clear, but julia of course complains (correctly) about an invalid assignment location. yes, special cases are ugly and confusing and should be created only very rarely.

currently I use

```julia
julia> (x==2) && ( y=2 )

```

but it looks a little too similar to `(x==2) && (y==2)` to me. I know that special cases are not particularly desirable. ‘and’ and ‘or’ would be better.

not important, but I think convenient.

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [May 17, 2018, 10:16pm UTC](https://discourse.julialang.org/t/syntactic-sugar-x-2-and-y-12-or-even-x-2-y-12/10969/2 "2018-05-17T22:16:34Z")

</div>

just to build on this. kind of similar to:

> [@How to do one-liners and other short conditionals?](https://discourse.julialang.org/t/how-to-do-one-liners-and-other-short-conditionals/6100/11):
>
> Allow me to short-circuit the discussion and point to the relevant issues: [#6823](https://github.com/JuliaLang/julia/issues/6823) and [#16389](https://github.com/JuliaLang/julia/issues/16389). Happy reading!

---

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [May 17, 2018, 11:59pm UTC](https://discourse.julialang.org/t/syntactic-sugar-x-2-and-y-12-or-even-x-2-y-12/10969/3 "2018-05-17T23:59:49Z")

</div>

glad to see it’s still open. I agree with stefan about the ‘and’ and ‘or’ (perl) style solution. /iaw

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [May 18, 2018, 2:04pm UTC](https://discourse.julialang.org/t/syntactic-sugar-x-2-and-y-12-or-even-x-2-y-12/10969/4 "2018-05-18T14:04:50Z")

</div>

It would also be possible to make the precedence of `=` relative to `&&` and `||` depend on whether it’s on the left or the right. We do that with some other operators and syntaxes.

---

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [May 18, 2018, 5:28pm UTC](https://discourse.julialang.org/t/syntactic-sugar-x-2-and-y-12-or-even-x-2-y-12/10969/5 "2018-05-18T17:28:41Z")

</div>

what scares me about context-dependent precedence is unforeseen consequences…I am not so smart as to anticipate everything that could happen in the future.

the “and” and “or” keywords seem safe, should be painless to implement, and do pretty much for the user what they are expected to do. I doubt that they would find a better use elsewhere.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [May 18, 2018, 6:22pm UTC](https://discourse.julialang.org/t/syntactic-sugar-x-2-and-y-12-or-even-x-2-y-12/10969/6 "2018-05-18T18:22:57Z")

</div>

That discussion was already had, however, and we decided not to add `and` and `or` keywords/infix operators.

---

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [May 20, 2018, 10:14pm UTC](https://discourse.julialang.org/t/syntactic-sugar-x-2-and-y-12-or-even-x-2-y-12/10969/7 "2018-05-20T22:14:20Z")

</div>

I will be happy to take anything. looking at my current project, about 1 in 10-20 of the lines in my code seem to consist of a condition deciding on an assignment, though not suitable for the trinary.

---

<div class="post-metadata">

**Author:** ![jeff.bezanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff.bezanson/32/48_2.png) [@jeff.bezanson](https://discourse.julialang.org/u/jeff.bezanson)\
**Post date:** [May 21, 2018, 4:39pm UTC](https://discourse.julialang.org/t/syntactic-sugar-x-2-and-y-12-or-even-x-2-y-12/10969/8 "2018-05-21T16:39:32Z")

</div>

A good solution here could be to allow `?` without `:`: `condition ? x = 2`.

---

<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 21, 2018, 4:53pm UTC](https://discourse.julialang.org/t/syntactic-sugar-x-2-and-y-12-or-even-x-2-y-12/10969/9 "2018-05-21T16:53:36Z")

</div>

+1 for this. I was surpised it didn’t work when i first used it, and I think it reads much better as a sentence than `&&`.

Although I do also like writing `x == 2 ? x = 1 : nothing` since it reads easiest, kind of like `where T <: Any` vs. `where T`.
