# Constant propagation outside functions

**URL:** https://discourse.julialang.org/t/constant-propagation-outside-functions/18457
**Category:** General Usage
**Tags:** question
**Created:** [December 8, 2018, 9:51am UTC](https://discourse.julialang.org/t/constant-propagation-outside-functions/18457 "2018-12-08T09:51:38Z")
**Posts on this page:** 3
**Page:** 1

<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: [December 8, 2018, 9:51am UTC](https://discourse.julialang.org/t/constant-propagation-outside-functions/18457/1 "2018-12-08T09:51:38Z")

</div>

I am confused about the rules of constant propagation in expressions which are outside functions. Specifically, my use case would be exploratory data analysis using building blocks of the DataFrames API, such as `groupby` etc, where I specify columns using symbols.

The MWE (using `DataFrames#master`) is

```julia
julia> VERSION
v"1.1.0-DEV.841"

julia> using DataFrames, Test

julia> df = DataFrame(a = 1:3, b = ones(3))
3×2 DataFrame
│ Row │ a │ b │
│ │ Int64 │ Float64 │
├─────┼───────┼─────────┤
│ 1 │ 1 │ 1.0 │
│ 2 │ 2 │ 1.0 │
│ 3 │ 3 │ 1.0 │

julia> @inferred getproperty(df, :a)
ERROR: return type Array{Int64,1} does not match inferred return type AbstractArray{T,1} where T
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] top-level scope at none:0

julia> @inferred identity(df.a)
3-element Array{Int64,1}:
 1
 2
 3

```

Why does it infer in one case but not the other?

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [December 8, 2018, 10:12am UTC](https://discourse.julialang.org/t/constant-propagation-outside-functions/18457/2 "2018-12-08T10:12:25Z")

</div>

This

> [@Tamas\_Papp](#):
>
> @inferred identity(df.a)

is equivalent to

```julia
x = df.a
@inferred identity(x)

```

which of course is fine.

---

<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: [December 8, 2018, 10:23am UTC](https://discourse.julialang.org/t/constant-propagation-outside-functions/18457/3 "2018-12-08T10:23:35Z")

</div>

Thanks, that clarifies it somewhat. The broader question is whether I need to wrap expressions in a function to get constant propagation at the top level when using symbols for columns keys.
