# Is Julia comprehension not a function call?

**URL:** <https://discourse.julialang.org/t/is-julia-comprehension-not-a-function-call/81541>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 23, 2022, 8:58pm UTC](https://discourse.julialang.org/t/is-julia-comprehension-not-a-function-call/81541 "2022-05-23T20:58:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliafriend](https://avatars.discourse-cdn.com/v4/letter/j/2acd7d/32.png) [@juliafriend](https://discourse.julialang.org/u/juliafriend)\
**Post date:** [May 23, 2022, 8:58pm UTC](https://discourse.julialang.org/t/is-julia-comprehension-not-a-function-call/81541/1 "2022-05-23T20:58:01Z")

</div>

If I parse a list comprehension as such:

 ![Screen Shot 2022-05-23 at 4.55.51 PM](https://global.discourse-cdn.com/julialang/original/3X/6/0/601d06aa16a7d4117705866e612a205febdc7fca.png)

1. The head isn’t a Symbol `call` but literally a `comprehension.` Is `call` for all non-built-in functions?

2. The first Arg for `comprehension` Expr isn’t a Symbol but an expression. `1: Expr`.  
How do we know which functions have Symbols as their first Arg and which have expressions? And why don’t all functions have Symbols as their first Arg?

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [May 23, 2022, 9:59pm UTC](https://discourse.julialang.org/t/is-julia-comprehension-not-a-function-call/81541/2 "2022-05-23T21:59:05Z")

</div>

1. `call` is for function calls. Comprehensions are not a function call (at one level of understanding, you can’t write a new method for comprehension)
2. The structure of `:comprehension` expressions is different to `:call`. But even `:call` might not have a symbol as their first argument:

```julia
julia> dump(:(a.x(1)))
Expr
  head: Symbol call
  args: Array{Any}((2,))
    1: Expr
      head: Symbol .
      args: Array{Any}((2,))
        1: Symbol a
        2: QuoteNode
          value: Symbol x
    2: Int64 1

```

Perhaps a better question to start with is, What are you trying to do?

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [May 23, 2022, 10:08pm UTC](https://discourse.julialang.org/t/is-julia-comprehension-not-a-function-call/81541/3 "2022-05-23T22:08:07Z")

</div>

You might also be interested in looking at the lowered IR. In general most syntax in julia corresponds to function calls, but they don’t show up until lowering:

```julia
julia> @Meta.lower [rand() for _ in 1:5, _ in 1:3]
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ $(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ global var"#9#10"
│ const var"#9#10"
│ %3 = Core._structtype(Main, Symbol("#9#10"), Core.svec(), Core.svec(), Core.svec(), false, 0)
│ Core._setsuper!(%3, Core.Function)
│ var"#9#10" = %3
│ Core._typebody!(%3, Core.svec())
└── return nothing
)))
│ %2 = Core.svec(var"#9#10", Core.Any)
│ %3 = Core.svec()
│ %4 = Core.svec(%2, %3, $(QuoteNode(:(#= none:0 =#))))
│ $(Expr(:method, false, :(%4), CodeInfo(
    @ none within `none`
1 ─ %1 = Base.indexed_iterate(@_2, 1)
│ Core.getfield(%1, 1)
│ @_3 = Core.getfield(%1, 2)
│ %4 = Base.indexed_iterate(@_2, 2, @_3)
│ Core.getfield(%4, 1)
│ %6 = rand()
└── return %6
)))
│ #9 = %new(var"#9#10")
│ %7 = #9
│ %8 = 1:5
│ %9 = 1:3
│ %10 = Base.product(%8, %9)
│ %11 = Base.Generator(%7, %10)
│ %12 = Base.collect(%11)
└── return %12
))))

```

There is a bit of junk here to set up the `(_,_)->rand()` closure, but the key part is at the bottom. You can see the comprehension essentially lowers to:  
`collect(Base.Generator((_, _)->rand(), Base.product(1:3, 1:3)))`

---

<div class="post-metadata">

**Author:** ![juliafriend](https://avatars.discourse-cdn.com/v4/letter/j/2acd7d/32.png) [@juliafriend](https://discourse.julialang.org/u/juliafriend)\
**Post date:** [May 26, 2022, 4:06pm UTC](https://discourse.julialang.org/t/is-julia-comprehension-not-a-function-call/81541/4 "2022-05-26T16:06:12Z")

</div>

> [@Keno](#):
>
> IR

Thank you! What is IR?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [May 26, 2022, 4:07pm UTC](https://discourse.julialang.org/t/is-julia-comprehension-not-a-function-call/81541/5 "2022-05-26T16:07:16Z")

</div>

intermediate representation: [Julia SSA-form IR · The Julia Language](https://docs.julialang.org/en/v1/devdocs/ssair/)

---

<div class="post-metadata">

**Author:** ![juliafriend](https://avatars.discourse-cdn.com/v4/letter/j/2acd7d/32.png) [@juliafriend](https://discourse.julialang.org/u/juliafriend)\
**Post date:** [May 26, 2022, 4:08pm UTC](https://discourse.julialang.org/t/is-julia-comprehension-not-a-function-call/81541/6 "2022-05-26T16:08:33Z")

</div>

Thank you!
