# 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:** 7\
**Page:** 5

<div class="post-metadata">

**Author:** ![younes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/younes/32/208506_2.png) [@younes](https://discourse.julialang.org/u/younes)\
**Post date:** [May 14, 2024, 4:11pm UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/82 "2024-05-14T16:11:16Z")

</div>

I like the `@view` macro (it’s super handy!) and the broadcasting with `.`

```julia-repl
julia> A = rand(5, 5)
5×5 Matrix{Float64}:
 0.707239 0.440889 0.958134 0.783449 0.913452
 0.741968 0.276487 0.489972 0.0194192 0.453019
 0.318276 0.318808 0.156329 0.268038 0.223835
 0.915922 0.743359 0.564748 0.60688 0.94491
 0.344369 0.136049 0.539166 0.962701 0.831359

julia> subA = @view A[2:4, 2:4]
3×3 view(::Matrix{Float64}, 2:4, 2:4) with eltype Float64:
 0.276487 0.489972 0.0194192
 0.318808 0.156329 0.268038
 0.743359 0.564748 0.60688

julia> subA .= 0
3×3 view(::Matrix{Float64}, 2:4, 2:4) with eltype Float64:
 0.0 0.0 0.0
 0.0 0.0 0.0
 0.0 0.0 0.0

julia> 

```

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [May 14, 2024, 4:28pm UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/83 "2024-05-14T16:28:20Z")

</div>

One of my biggest pain points in Julia is that `[...]` makes a copy.

Using the macro in nested expressions needs parentheses which makes it not as quick to type:

```julia
julia> .+(x[1:3], x[2:4])
3-element Vector{Float64}:
 -0.7509311698949941
  1.709855739076295
  0.7021749402293793

julia> .+(@view x[1:3], @view x[2:4])
ERROR: LoadError: ArgumentError: Invalid use of @view macro: argument must be a reference expression A[...].
Stacktrace:
 [1] var"@view"( __source__ ::LineNumberNode, __module__ ::Module, ex::Any)
   @ Base ./views.jl:138
in expression starting at REPL[11]:1

julia> .+(@view(x[1:3]), @view(x[2:4]))
3-element Vector{Float64}:
 -0.7509311698949941
  1.709855739076295
  0.7021749402293793

julia> .+(@view(x[1:3]), @view(x[2:4]))

```

I wonder actually, why this does not work. The `@.` propagates fully into the whole call, `@view` does not:

```julia
julia> @view x[1:3] .+ x[1:4]
ERROR: LoadError: ArgumentError: Invalid use of @view macro: argument must be a reference expression A[...].
Stacktrace:
 [1] var"@view"( __source__ ::LineNumberNode, __module__ ::Module, ex::Any)
   @ Base ./views.jl:138
in expression starting at REPL[13]:1

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 14, 2024, 4:34pm UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/84 "2024-05-14T16:34:54Z")

</div>

Try `@views` instead of `@view`. That should apply to the whole expression. You can even apply it to a block, such as an entire function.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [May 14, 2024, 4:38pm UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/85 "2024-05-14T16:38:46Z")

</div>

> [@roflmaostc](#):
>
> which makes it not as quick to type

Isn’t it the case for @views?

---

<div class="post-metadata">

**Author:** ![younes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/younes/32/208506_2.png) [@younes](https://discourse.julialang.org/u/younes)\
**Post date:** [May 14, 2024, 9:10pm UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/86 "2024-05-14T21:10:52Z")

</div>

I’m writing this to synthesize what I learnt thanks to you guys (and the Julia source code & docs). My understanding is that:

`@view` expects an array-indexing expression =\> you must give it something like `x[1:3]` or `x[:]` (throws an error otherwise)

`@views` expects “any” expression and transform array-slicing operations into views =\> cf. below (have been playing with `@views`)

```julia-repl
julia> @views 1+2+3; "This is noise"
"This is noise"

julia> @views 1+2+3; "This is noise"; A
3×3 Matrix{Float64}:
 1.0 1.0 1.0
 2.0 2.0 2.0
 3.0 3.0 3.0

julia> @views 1+2+3; "This is noise"; A[:]
9-element Vector{Float64}:
 1.0
 2.0
 3.0
 1.0
 2.0
 3.0
 1.0
 2.0
 3.0

julia> A
3×3 Matrix{Float64}:
 1.0 1.0 1.0
 2.0 2.0 2.0
 3.0 3.0 3.0

```

and finally (as @DNF and @rafael.guerra suggested), voilà

```julia-repl
julia> @views .+(x[1:3], x[2:4])
3-element Vector{Float64}:
 0.6605174669013307
 0.8487232659913083
 1.2442778601207747

```

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [May 14, 2024, 9:34pm UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/87 "2024-05-14T21:34:07Z")

</div>

> [@younes](#):
>
> ```julia
> julia> @views 1+2+3; "This is noise"; A[:]
> 
> ```

The `@views` stops at the ;. Add parentheses, or `begin`/`end` as mentioned earlier, if you want it to apply across the ;

```julia
julia> @macroexpand ( @views 1+2; a[1] )
quote
    1 + 2
    #= REPL[8]:1 =#
    a[1]
end

julia> @macroexpand @views ( 1+2; a[1] )
quote
    1 + 2
    #= REPL[9]:1 =#
    (Base.maybeview)(a, 1)
end

julia> @macroexpand @views begin 1+2; a[1]; end
quote
    #= REPL[11]:1 =#
    1 + 2
    #= REPL[11]:1 =#
    (Base.maybeview)(a, 1)
end

```

---

<div class="post-metadata">

**Author:** ![younes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/younes/32/208506_2.png) [@younes](https://discourse.julialang.org/u/younes)\
**Post date:** [May 15, 2024, 6:41am UTC](https://discourse.julialang.org/t/whats-your-favorite-syntactical-sugar-in-julia/49781/88 "2024-05-15T06:41:20Z")

</div>

thank you for the clarification 👍🏼

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