# What's your favorite syntactical sugar in Julia

**URL:** https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781
**Category:** Teaching & Outreach
**Tags:** question
**Created:** [November 8, 2020, 10:33am UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781 "2020-11-08T10:33:13Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [November 8, 2020, 10:33am UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/1 "2020-11-08T10:33:13Z")

</div>

Hi best community!

I’m planning of doing a Julia Lang tutorial series around syntactical sugar in Julia (feel free to steal the idea). Before that I want to ask you the community what your favorite syntactical sugar in Julia is 🙂

I really looking forward for all your answers. Thanks!

---

<div class="post-metadata">

### Author: ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discourse.julialang.org/u/Volker)
#### Post date: [November 8, 2020, 11:03am UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/2 "2020-11-08T11:03:47Z")

</div>

I really like the `...`-operator. I.e. for

```julia
nested = [[1,2,3],[4,5]]
vcat(nested...)

```

or

```julia
plots = [plot(rand(3,1), rand(3,1)) for i in 1:10]
plot([plots...])

```

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [November 8, 2020, 11:21am UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/3 "2020-11-08T11:21:55Z")

</div>

`XXX"some string"`

for

`@XXX_str "some string")`

---

<div class="post-metadata">

### Author: ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)
#### Post date: [November 8, 2020, 11:46am UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/4 "2020-11-08T11:46:39Z")

</div>

`xxx |> typeof`

`xxx |> dump`

---

<div class="post-metadata">

### Author: ![anon92994695](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@anon92994695](https://discourse.julialang.org/u/anon92994695)
#### Post date: [November 8, 2020, 11:52am UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/5 "2020-11-08T11:52:30Z")

</div>

The broadcast operator really does it for me. It makes code so much more succinct and readable. Has probably saved me days of my life writing code at this point.

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [November 8, 2020, 1:06pm UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/6 "2020-11-08T13:06:21Z")

</div>

If you are considering packages, `JuMP.jl` has a nice mechanism of choosing the best container for your variables/constrains depending on the range you pass to it (if it is a `1:N` range it is a `Vector`, if it is `X:N` it is a non-array dense container, if it has holes it is yet another container). Most users never perceive that.

---

<div class="post-metadata">

### Author: ![anon92994695](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@anon92994695](https://discourse.julialang.org/u/anon92994695)
#### Post date: [November 8, 2020, 2:08pm UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/7 "2020-11-08T14:08:03Z")

</div>

I was just using JuMP yesterday, and I was a little upset finding tutorials with deprecated syntax everywhere. Then I started realizing what changed and why - all I can say is “thank you”. I’ll second the clever and hard work that’s been done to make JuMP more Julian and easy to use.

---

<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 8, 2020, 2:48pm UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/8 "2020-11-08T14:48:44Z")

</div>

I second @anon92994695’s suggestion broadcasting. One simple `.` lifts functions into a different realm, invoking an [elaborate, customizable framework](https://julialang.org/blog/2018/05/extensible-broadcast-fusion/) that can yield huge optimizations.

Also, it is simply the right answer to the question every numerically oriented language faced at some point so far, ie which operations should “vectorize”. The Julian answer is _none of them_, because `f.` will “vectorize” every possible function _and_ [fuse loops](https://julialang.org/blog/2017/01/moredots/).

Other than that, I think that compared to some languages, Julia has been quite parsimonious with syntactic sugar, and it (again) is the right decision (at this point, astute readers can tell that I happen to like the language). You need a bit of sugar for the important things, otherwise you just want composable zero-cost abstractions.

---

<div class="post-metadata">

### Author: ![roble](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roble/32/12303_2.png) [@roble](https://discourse.julialang.org/u/roble)
#### Post date: [November 8, 2020, 4:19pm UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/9 "2020-11-08T16:19:21Z")

</div>

I enjoy using the `do` syntax. It makes anonymous functions much more readable to me.

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [November 8, 2020, 4:43pm UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/10 "2020-11-08T16:43:10Z")

</div>

I think the getproperty / getfield methods for dot notation as well as getindex / setindex! for bracket notation are pretty cool and allow a lot of extensions

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [November 8, 2020, 4:52pm UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/11 "2020-11-08T16:52:41Z")

</div>

Yea I agree with dot broadcasting and splatting. My code is littered with these.

I also like generators if we are considering those as sugar as well. Same with `@views`.

---

<div class="post-metadata">

### Author: ![josePereiro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josepereiro/32/17322_2.png) [@josePereiro](https://discourse.julialang.org/u/josePereiro)
#### Post date: [November 8, 2020, 10:22pm UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/12 "2020-11-08T22:22:06Z")

</div>

A recent addition I REALLY love:

```julia
fun(;name = "John Doe") = println("Hi ", name)
# before
name = "Robert"
fun(;name = name)
# now
fun(;name)

```

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [November 9, 2020, 12:36am UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/13 "2020-11-09T00:36:33Z")

</div>

> [@Gnimuc](#):
>
> `xxx |> typeof`
> 
> `xxx |> dump`

this is nuttier

`(xxx, yyy) .|> (typeof, dump)` is equivalent to `(typeof(xxx), dump(yyy))`.

See

`[[1,2], [3,4]] .|> (sum, maximum) # give [3, 4]`

---

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [November 9, 2020, 12:37am UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/14 "2020-11-09T00:37:49Z")

</div>

Packing named arguments is my favorite as well. I would love it they add the natural extention of this to _unpacking_ named arguments too:

```julia
# pack named or unnamed arguments
foo(x,y)
foo(;x,y)

# unpack named or unnamed arguments
(x,y) = bar()
(;x,y) = bar()

```

Currently the first three cases work, it just makes sense to me that the fourth should as well.

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [November 9, 2020, 12:46am UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/15 "2020-11-09T00:46:13Z")

</div>

how is the fourth different to the 3rd?

---

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [November 9, 2020, 12:49am UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/16 "2020-11-09T00:49:41Z")

</div>

It would unpack based on the names rather than based on order, so it’d be equivalent to `tmp=bar(); x=tmp.x; y=tmp.y`. (currently you can use UnPack.jl’s `@unpack x,y = bar()` to do this, but I just think this functionality fits into base Julia really well).

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [November 9, 2020, 4:45am UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/17 "2020-11-09T04:45:18Z")

</div>

`f ∘ g`

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [November 9, 2020, 4:47am UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/18 "2020-11-09T04:47:33Z")

</div>

```julia
1 ∉ (2, 3)

```

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [November 9, 2020, 4:53am UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/19 "2020-11-09T04:53:40Z")

</div>

```julia
[1 2
 3 4]

```

which is sugar for `hvcat`.

---

<div class="post-metadata">

### Author: ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)
#### Post date: [November 9, 2020, 5:16am UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/20 "2020-11-09T05:16:54Z")

</div>

```julia
using Distributions

Dist=[LogNormal(), Gamma(), Truncated(Normal(),0,1)]
Stat=[mean, std, entropy]
[f(D) for f ∈ Stat, D ∈ Dist]

```

🕶 🕶 🕶

[Next page](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781.md?page=2)
