# Is there a specific reason why the const keyword cannot be used within the scope of a function?

**URL:** <https://discourse.julialang.org/t/is-there-a-specific-reason-why-the-const-keyword-cannot-be-used-within-the-scope-of-a-function/123986>\
**Category:** Performance\
**Tags:** question\
**Created:** [December 19, 2024, 9:58am UTC](https://discourse.julialang.org/t/is-there-a-specific-reason-why-the-const-keyword-cannot-be-used-within-the-scope-of-a-function/123986 "2024-12-19T09:58:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [December 19, 2024, 9:58am UTC](https://discourse.julialang.org/t/is-there-a-specific-reason-why-the-const-keyword-cannot-be-used-within-the-scope-of-a-function/123986/1 "2024-12-19T09:58:33Z")

</div>

I noticed that trying to use the const keyword in a local scope results in a compilation error. Is there a specific reason for this? I wanted to define an anonymous function in a local scope (see [Creating an anonymous function at the local level. Conflict with const resulting in performance issues - #5 by stefkuypers](https://discourse.julialang.org/t/creating-an-anonymous-function-at-the-local-level-conflict-with-const-resulting-in-performance-issues/123921/5) for details) since it was an elegant solution to a problem I was facing but the performance hit I got due to the fact that I could not make that function a const made it useless. So now I’m curious as to why the use of the const keyword is not allowed in a local scope.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [December 19, 2024, 10:11am UTC](https://discourse.julialang.org/t/is-there-a-specific-reason-why-the-const-keyword-cannot-be-used-within-the-scope-of-a-function/123986/2 "2024-12-19T10:11:18Z")

</div>

No, it could be done.

> <https://github.com/JuliaLang/julia/issues/5148#issuecomment-320319147>
>
> I am wondering why code:
> 
> \`\`\`
> function f()
> const x = "aaa"
> x = 4…
> return x
> end
> println(f())
> \`\`\`
> 
> executes and prints 4 without any warning or error, while
> 
> \`\`\`
> const x = "aaa"
> x = 4
> println(x)
> \`\`\`
> 
> raises:
> \`ERROR: invalid redefinition of constant x\`
> 
> Similarly:
> 
> \`\`\`
> function f()
> const x = "aaa"
> x = "bbb"
> return x
> end
> println(f())
> \`\`\`
> 
> prints bbb without warning, while:
> 
> \`\`\`
> const x = "aaa"
> x = "bbb"
> println(x)
> \`\`\`
> 
> prints bbb, but with warning:
> \`Warning: redefining constant x\`
> 
> I think behavior of constants should be consistent in global and function scopes.
> 
> My Julia version:
> 
> \`\`\`
> Julia Version 0.2.0
> Commit 05c6461 (2013-11-16 23:44 UTC)
> Platform Info:
> System: Windows (x86\_64-w64-mingw32)
> WORD\_SIZE: 64
> BLAS: libopenblas (USE64BITINT DYNAMIC\_ARCH NO\_AFFINITY)
> LAPACK: libopenblas
> LIBM: libopenlibm
> \`\`\`

It actually used to be ignored before v1, and it was made into an error to allow reintroduction without breaking code. It’s not here yet because they haven’t figured out how it can work and why it should work. For example,

> [@stefkuypers](#):
>
> the performance hit I got due to the fact that I could not make that function a const

this is not true. Only variables can be `const`, not objects like functions. You already do not reassign the local variable `sim_function` in the method `init_model`, the only thing that `const` would enforce, and `const`-ness makes zero difference when passed into a method call `Model(sim_function)`. There are likely other reasons for the performance hit, the most immediate one is the inherent type instability of `struct Model sim_function::Function end`, which foobar\_lv2 has already explained.

---

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [December 19, 2024, 10:27am UTC](https://discourse.julialang.org/t/is-there-a-specific-reason-why-the-const-keyword-cannot-be-used-within-the-scope-of-a-function/123986/3 "2024-12-19T10:27:36Z")

</div>

The following code clearly proves that functions can be made const too.

```julia
using BenchmarkTools

foo1 = x -> x + 2
const foo2 = x -> x + 2

@btime for i in 1:10000; foo1(i); end

@btime for i in 1:10000; foo2(i); end

```

On my computer, the first execution results in the following output:  
143.292 μs (18980 allocations: 296.56 KiB)

The second execution results in:  
0.916 ns (0 allocations: 0 bytes)

I’d say that is quite a performance hit for not using the const keyword with an anonymous function.

As for the remark on the Function type, when I use a named function instead of an non const anonymous function I don’t have the performance penalty.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [December 19, 2024, 10:37am UTC](https://discourse.julialang.org/t/is-there-a-specific-reason-why-the-const-keyword-cannot-be-used-within-the-scope-of-a-function/123986/4 "2024-12-19T10:37:48Z")

</div>

No it doesn’t, you’re using BenchmarkTools wrong and misinterpreting the outcome. If we follow its documentation on treating non-`const` global variables:

```julia
julia> using BenchmarkTools

julia> foo1 = x -> x + 2
#1 (generic function with 1 method)

julia> const foo2 = x -> x + 2
#3 (generic function with 1 method)

julia> @btime for i in 1:10000; $foo1(i); end
  1.700 ns (0 allocations: 0 bytes)

julia> @btime for i in 1:10000; $foo2(i); end
  2.900 ns (0 allocations: 0 bytes)

```

All those allocations are gone, in fact the “const” `foo2` ran a bit slower (the difference is just within OS randomness). Now let’s flip it around with the incorrect usage:

```julia
julia> const bar1 = foo1
#1 (generic function with 1 method)

julia> bar2 = foo2
#3 (generic function with 1 method)

julia> @btime for i in 1:10000; bar1(i); end
  1.300 ns (0 allocations: 0 bytes)

julia> @btime for i in 1:10000; bar2(i); end
  293.300 μs (18980 allocations: 296.56 KiB)

julia> bar1 === foo1, bar2 === foo2
(true, true)

```

Now the non-`const` `bar2` took a performance hit, but it is assigned to the exact same function as `const foo2`. `const`-ness is about variables, not functions, and there’s a massive difference between variables being shared across scopes and functions being passed as arguments.

> [@stefkuypers](#):
>
> As for the remark on the Function type, when I use a named function instead of an non const anonymous function I don’t have the performance penalty.

Named functions inside local scopes aren’t `const` and neither are the names, as you can easily reassign them without erroring.

---

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [December 19, 2024, 10:52am UTC](https://discourse.julialang.org/t/is-there-a-specific-reason-why-the-const-keyword-cannot-be-used-within-the-scope-of-a-function/123986/5 "2024-12-19T10:52:01Z")

</div>

Ok, thanks. I seriously need to wrap my head around this!

Why is bar2 taking a performance hit now???

Ok, hold on. bar1 and bar2 are variables, right? And thus in the bar example bar1 is the const variable and bar2 is the non-const variable. Ok, I think I get it now.
