# Introduce a command-line option to warn on constant redefinition?

**URL:** https://discourse.julialang.org/t/introduce-a-command-line-option-to-warn-on-constant-redefinition/126213
**Category:** Internals & Design
**Tags:** feature-request, command-line-options
**Created:** [February 23, 2025, 1:30pm UTC](https://discourse.julialang.org/t/introduce-a-command-line-option-to-warn-on-constant-redefinition/126213 "2025-02-23T13:30:40Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [February 23, 2025, 1:30pm UTC](https://discourse.julialang.org/t/introduce-a-command-line-option-to-warn-on-constant-redefinition/126213/1 "2025-02-23T13:30:40Z")

</div>

Keno’s recent changes on in-development versions of Julia enable safe redefinition of constants, including, for example, `struct` types. This is great, especially for interactive use, Revise.jl, etc.

However, I think it’d be good if there was a command-line option to warn on the redefinition of a constant, furthermore, this should probably be turned on by default for package tests.

It was always common for `include` to be misused (including the same source file repeatedly), and `--warn-overwrite=yes` is helpful in this regard, as it warns on method overwrite, however now it will be possible to redefine any constant, so the failure modes on `include`-misuse are expanded.

I propose either of these changes:

- add a new command-line option, say `--warn-redefine-constant=yes`, and enable it by default for package tests
- adjust `--warn-overwrite=yes` to warn on the redefinition of any constant

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [February 23, 2025, 2:30pm UTC](https://discourse.julialang.org/t/introduce-a-command-line-option-to-warn-on-constant-redefinition/126213/2 "2025-02-23T14:30:04Z")

</div>

Probably a good idea to allow by default in the REPL and error by default in a script. People seem to think it’s ok to do things that emit warnings for some reason. Of course now it’s actually fairly safe, just potentially slow since it invalidates code.

---

<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: [February 23, 2025, 4:12pm UTC](https://discourse.julialang.org/t/introduce-a-command-line-option-to-warn-on-constant-redefinition/126213/3 "2025-02-23T16:12:19Z")

</div>

> [@nsajko](#):
>
> enable safe redefinition of constants, including, for example, `struct` types.

How far does it go? I can imagine backedges can help change methods that access those constants upon execution, whatever runtime errors those may cause, but does it also change accesses in already evaluated method/struct definitions? For example, reassigning a non-type to a constant type name might still be callable, but it won’t make sense in annotations and type bounds. Even if we restrict type names to types, reassigning a concrete type to an abstract type’s name can result in concrete supertyping in other struct definitions.

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [February 23, 2025, 9:20pm UTC](https://discourse.julialang.org/t/introduce-a-command-line-option-to-warn-on-constant-redefinition/126213/4 "2025-02-23T21:20:22Z")

</div>

@Benny, pretty sure method invalidation is the answer. When a constant changes all dependent method just have to get recompiled. I guess.

---

<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: [February 24, 2025, 1:34am UTC](https://discourse.julialang.org/t/introduce-a-command-line-option-to-warn-on-constant-redefinition/126213/5 "2025-02-24T01:34:23Z")

</div>

That’s the part I can see happening, e.g. `foo() = AbstractType()` getting invalidated. I’m asking about something like `foo(x::AbstractType) = x` or `struct Bar <: AbstractType end`; `AbstractType = 1` should fail for both cases, and `AbstractType = Int` should fail for the latter.

---

<div class="post-metadata">

### Author: ![tecosaur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tecosaur/32/23206_2.png) [@tecosaur](https://discourse.julialang.org/u/tecosaur)
#### Post date: [February 24, 2025, 2:49am UTC](https://discourse.julialang.org/t/introduce-a-command-line-option-to-warn-on-constant-redefinition/126213/6 "2025-02-24T02:49:48Z")

</div>

I’ve noticed these don’t seem to be Revise-d at the moment, I’d think this either means more back-edges are needed, or when redefining a struct something like `methodswith` needs to be called.

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [February 24, 2025, 9:01am UTC](https://discourse.julialang.org/t/introduce-a-command-line-option-to-warn-on-constant-redefinition/126213/7 "2025-02-24T09:01:36Z")

</div>

Issue:

> <https://github.com/JuliaLang/julia/issues/57515>
>
> These two both throw in \`show\`:
> 
> \`\`\`julia
> abstract type AbstractType end
> foo(x::…AbstractType) = x
> const AbstractType = 1
> methods(foo)
> \`\`\`
> 
> \`\`\`julia
> abstract type AbstractType end
> struct Bar \<: AbstractType end
> const AbstractType = 1
> supertype(Bar)
> \`\`\`
> 
> cc @Keno
> 
> Pointed out on Discourse by Benny:
> \* https://discourse.julialang.org/t/introduce-a-command-line-option-to-warn-on-constant-redefinition/126213/5?u=nsajko
