# Cannot declare Main.foo constant; it already has a value

**URL:** https://discourse.julialang.org/t/cannot-declare-main-foo-constant-it-already-has-a-value/112453
**Category:** Internals & Design
**Created:** [April 3, 2024, 6:47am UTC](https://discourse.julialang.org/t/cannot-declare-main-foo-constant-it-already-has-a-value/112453 "2024-04-03T06:47:05Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [April 3, 2024, 6:47am UTC](https://discourse.julialang.org/t/cannot-declare-main-foo-constant-it-already-has-a-value/112453/1 "2024-04-03T06:47:05Z")

</div>

I experiment with setting the value of `foo`, decide I’m happy with it, and I want to make it `const` but I can’t. Can something be done to make this less annoying?

```julia
julia> foo = []
Any[]

julia> const foo = []
ERROR: cannot declare Main.foo constant; it already has a value

```

---

<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: [April 3, 2024, 7:05am UTC](https://discourse.julialang.org/t/cannot-declare-main-foo-constant-it-already-has-a-value/112453/2 "2024-04-03T07:05:17Z")

</div>

I proposed a similar mechanism recently, but the problem is that functions may have been compiled previously that assume that the binding is mutable. We don’t track those kinds of edges currently, so it is not possible to make a binding `const` after the fact. It’d need something like the world-age dependent bindings table that I proposed for the struct redefinition problem.

---

<div class="post-metadata">

### Author: ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)
#### Post date: [April 15, 2024, 1:38am UTC](https://discourse.julialang.org/t/cannot-declare-main-foo-constant-it-already-has-a-value/112453/3 "2024-04-15T01:38:08Z")

</div>

Should we downgrade this to a warning? It is not like the code can be caused to misbehave by failing to convert it to a const binding; so it is safe to ignore that, even if the user might find that to be incongruous of it.

---

<div class="post-metadata">

### Author: ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)
#### Post date: [April 15, 2024, 2:04am UTC](https://discourse.julialang.org/t/cannot-declare-main-foo-constant-it-already-has-a-value/112453/4 "2024-04-15T02:04:17Z")

</div>

Actually, you can cause misbehavior by failing to convert it to a const binding.

After declaring without error that `const foo = [],` what should the following code return: `isconst(Main, :foo)`? Obviously, if you are only referring to using the value of `foo` then you cannot cause any misbehavior, but there are other behaviors that might go past that (even if those are not so frequent).

---

<div class="post-metadata">

### Author: ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)
#### Post date: [April 17, 2024, 2:45pm UTC](https://discourse.julialang.org/t/cannot-declare-main-foo-constant-it-already-has-a-value/112453/5 "2024-04-17T14:45:15Z")

</div>

That is probably undesirable behavior by the user, but it doesn’t exhibit any significant misbehavior for the compiler
