# \#constants

**URL:** https://discourse.julialang.org/tag/constants/945.md

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

---

## [Why does LLVM like putting floating-point constants into RODATA so much? Is that suboptimal?](https://discourse.julialang.org/t/why-does-llvm-like-putting-floating-point-constants-into-rodata-so-much-is-that-suboptimal/130314)

<div class="topic-metadata">

**Author:** [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Replies:** 1\
**Last updated:** [June 29, 2025, 8:36pm UTC](https://discourse.julialang.org/t/why-does-llvm-like-putting-floating-point-constants-into-rodata-so-much-is-that-suboptimal/130314 "2025-06-29T20:36:46Z")

</div>

It is almost impossible to avoid LLVM storing operands in floating-point operations into the data segment, RODATA, of the executable. For example, here the compiler converts 3 into Float64, storing the bytes into the da…

---

## [Global constants](https://discourse.julialang.org/t/global-constants/124105)

<div class="topic-metadata">

**Author:** [@langestefan](https://discourse.julialang.org/u/langestefan)\
**Replies:** 6\
**Last updated:** [December 23, 2024, 6:14pm UTC](https://discourse.julialang.org/t/global-constants/124105 "2024-12-23T18:14:40Z")

</div>

What is the best (readable, performant) way to store global constants? For example I want to store the Boltzmann constant in a central place and then use it across my package. The compiler would be able to fill this in…

---

## [Package Development: Why is Ref needed to pass a global constant to a function?](https://discourse.julialang.org/t/package-development-why-is-ref-needed-to-pass-a-global-constant-to-a-function/122566)

<div class="topic-metadata">

**Author:** [@Tetrakai](https://discourse.julialang.org/u/Tetrakai)\
**Replies:** 13\
**Last updated:** [November 15, 2024, 7:47pm UTC](https://discourse.julialang.org/t/package-development-why-is-ref-needed-to-pass-a-global-constant-to-a-function/122566 "2024-11-15T19:47:05Z")

</div>

I currently have the following setup (which works great) but somehow seems inelegant: src/MyPackage.jl module MyPackage include("mysrc.jl") const CONFIG = Ref{Config}() end src/mysrc.jl @with\_kw struct Config a :…

---

## [How to change constant variable inside a package for testing](https://discourse.julialang.org/t/how-to-change-constant-variable-inside-a-package-for-testing/110739)

<div class="topic-metadata">

**Author:** [@Maysam\_Gholampour](https://discourse.julialang.org/u/Maysam_Gholampour)\
**Replies:** 6\
**Last updated:** [February 27, 2024, 3:14am UTC](https://discourse.julialang.org/t/how-to-change-constant-variable-inside-a-package-for-testing/110739 "2024-02-27T03:14:51Z")

</div>

I am developing a new package. In package I have defined couple of constant variables. as it is not possible to redefine constant variable, I can’t change the constant variables for testing. we need to reset the Julia. I…

---

## [Is there a way to impose a constraint on the output of a Flux neural network model?](https://discourse.julialang.org/t/is-there-a-way-to-impose-a-constraint-on-the-output-of-a-flux-neural-network-model/100250)

<div class="topic-metadata">

**Author:** [@schwob](https://discourse.julialang.org/u/schwob)\
**Replies:** 4\
**Last updated:** [June 15, 2023, 3:24pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-impose-a-constraint-on-the-output-of-a-flux-neural-network-model/100250 "2023-06-15T15:24:23Z")

</div>

Suppose that I have the observed data y\_i\\sim N(0,1/w(x\_i)), where x\_i=i for i=1,...,100. I define the true function w(x\_i)=(x\_i-50)^2 and hope to recover this function with a Flux neural network model. Let’s denote the…

---

## [Globals / Constants Act Differently Depending on Use?](https://discourse.julialang.org/t/globals-constants-act-differently-depending-on-use/93933)

<div class="topic-metadata">

**Author:** [@dkzwart](https://discourse.julialang.org/u/dkzwart)\
**Replies:** 2\
**Last updated:** [February 2, 2023, 5:38pm UTC](https://discourse.julialang.org/t/globals-constants-act-differently-depending-on-use/93933 "2023-02-02T17:38:45Z")

</div>

I’ve been doing a lot of reading on Constants and Globals, and I realize I really don’t understand what I am reading. Having said that, can you all help me understand why var1 is undefined in the function? It seems to be…

---

## [Destructuring syntax does not respect const annotation](https://discourse.julialang.org/t/destructuring-syntax-does-not-respect-const-annotation/88751)

<div class="topic-metadata">

**Author:** [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Replies:** 2\
**Last updated:** [October 14, 2022, 8:55pm UTC](https://discourse.julialang.org/t/destructuring-syntax-does-not-respect-const-annotation/88751 "2022-10-14T20:55:32Z")

</div>

The const keyword seems to have no effect when used with the destructuring syntax in Julia 1.7+. julia\> t = (;a = 1, b = 2); julia\> const (;a, b) = t (a = 1, b = 2) julia\> a = 3 # re-definition produces no error or wa…

---

## [\`im\` as global constant](https://discourse.julialang.org/t/im-as-global-constant/84055)

<div class="topic-metadata">

**Author:** [@kurt](https://discourse.julialang.org/u/kurt)\
**Replies:** 2\
**Last updated:** [July 11, 2022, 3:22pm UTC](https://discourse.julialang.org/t/im-as-global-constant/84055 "2022-07-11T15:22:10Z")

</div>

Where is im as a global constant defined? I’ve checked base.jl, complex.jl and mathconsts.jl. I cant seem to find it. Help would be appreciated. Thank you

---

## [Efficiently passing a set of constants to a function](https://discourse.julialang.org/t/efficiently-passing-a-set-of-constants-to-a-function/72248)

<div class="topic-metadata">

**Author:** [@bgctw](https://discourse.julialang.org/u/bgctw)\
**Replies:** 21\
**Last updated:** [July 8, 2022, 7:25pm UTC](https://discourse.julialang.org/t/efficiently-passing-a-set-of-constants-to-a-function/72248 "2022-07-08T19:25:15Z")

</div>

I am using physical constants in a function in a package. The access to these constants should be fast, best the compiler could replace the values in the lower-level-code. I can actually achieve this by using global con…

---

## [Are "constants" constant or not](https://discourse.julialang.org/t/are-constants-constant-or-not/81988)

<div class="topic-metadata">

**Author:** [@earnest](https://discourse.julialang.org/u/earnest)\
**Replies:** 8\
**Last updated:** [May 31, 2022, 5:16pm UTC](https://discourse.julialang.org/t/are-constants-constant-or-not/81988 "2022-05-31T17:16:20Z")

</div>

i am a hobbyist, saw and liked Julia and then started reading, and exercising with it. one of the things i like to have in my simple programs is a constant, like avogadro constant, which lacks in python - my favourite to…

---

## [Where to put a bunch of constants?](https://discourse.julialang.org/t/where-to-put-a-bunch-of-constants/76885)

<div class="topic-metadata">

**Author:** [@marianoarnaiz](https://discourse.julialang.org/u/marianoarnaiz)\
**Replies:** 3\
**Last updated:** [February 22, 2022, 5:49pm UTC](https://discourse.julialang.org/t/where-to-put-a-bunch-of-constants/76885 "2022-02-22T17:49:07Z")

</div>

Hi everyone. I am developing a set of scripts for geophysics in Julia and at the core of many of my codes I have a bunch of values (maybe they can be consider Julia Constants at some point) but in general they are physi…

---

## [How to desctructure a namedtuple and declare the variables to be constants using the new destructuring syntax?](https://discourse.julialang.org/t/how-to-desctructure-a-namedtuple-and-declare-the-variables-to-be-constants-using-the-new-destructuring-syntax/76830)

<div class="topic-metadata">

**Author:** [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Replies:** 1\
**Last updated:** [February 21, 2022, 6:15am UTC](https://discourse.julialang.org/t/how-to-desctructure-a-namedtuple-and-declare-the-variables-to-be-constants-using-the-new-destructuring-syntax/76830 "2022-02-21T06:15:12Z")

</div>

julia\> const nt = (; a = 1) (a = 1,) julia\> const a\_ = nt.a 1 This defines a\_ as a constant. Now julia v1.7 has introduced the new destructuring syntax julia\> (; a) = nt (a = 1,) Is there any way to use this syntax a…

---

## [Const vs zero argument function](https://discourse.julialang.org/t/const-vs-zero-argument-function/76770)

<div class="topic-metadata">

**Author:** [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Replies:** 4\
**Last updated:** [February 19, 2022, 9:19pm UTC](https://discourse.julialang.org/t/const-vs-zero-argument-function/76770 "2022-02-19T21:19:57Z")

</div>

In Julia, we have both trivial zero argument functions and constants a() = 7 const A = 8 Both support constant propagation: f(x) = x+a() g(x) = x+A @code\_llvm f(0) ; @ REPL\[3\]:1 within \`f\` define i64 @julia\_f\_140(i64…

---

## [Typeconst proposal](https://discourse.julialang.org/t/typeconst-proposal/75953)

<div class="topic-metadata">

**Author:** [@lewis](https://discourse.julialang.org/u/lewis)\
**Replies:** 33\
**Last updated:** [February 9, 2022, 8:34pm UTC](https://discourse.julialang.org/t/typeconst-proposal/75953 "2022-02-09T20:34:35Z")

</div>

Great discussion, which added a lot of nuances and explicit advice based on use cases. The difficulty of expressing this so clearly might suggest that the keyword const is a misnomer, though it helps the Julia compiler …

---

## [Const variables](https://discourse.julialang.org/t/const-variables/42956)

<div class="topic-metadata">

**Author:** [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Replies:** 29\
**Last updated:** [July 13, 2020, 10:57pm UTC](https://discourse.julialang.org/t/const-variables/42956 "2020-07-13T22:57:41Z")

</div>

I am sure this issue has been debated before. By now, I have learned that all global variables must be constants. But I am less clear on global variables that are a function of other global variables, already defined as…

---

## [How to find where constants are defined](https://discourse.julialang.org/t/how-to-find-where-constants-are-defined/74936)

<div class="topic-metadata">

**Author:** [@wsshin](https://discourse.julialang.org/u/wsshin)\
**Replies:** 6\
**Last updated:** [January 20, 2022, 4:43pm UTC](https://discourse.julialang.org/t/how-to-find-where-constants-are-defined/74936 "2022-01-20T16:43:38Z")

</div>

Is there a way to reveal the file where constants are defined? For example, we have π = 3.1415926535897..., but which file defines this constant? I am looking for something like @less for functions.

---

## [Best way to handle user constants (e.g. API key)?](https://discourse.julialang.org/t/best-way-to-handle-user-constants-e-g-api-key/20912)

<div class="topic-metadata">

**Author:** [@dopamine](https://discourse.julialang.org/u/dopamine)\
**Replies:** 7\
**Last updated:** [December 18, 2020, 10:33am UTC](https://discourse.julialang.org/t/best-way-to-handle-user-constants-e-g-api-key/20912 "2020-12-18T10:33:36Z")

</div>

When developing a package, what is the convention/best way to handle user constants? e.g. API key, user name, etc.

---

## [Testing different constant definitions](https://discourse.julialang.org/t/testing-different-constant-definitions/50376)

<div class="topic-metadata">

**Author:** [@Luapulu](https://discourse.julialang.org/u/Luapulu)\
**Replies:** 5\
**Last updated:** [November 18, 2020, 6:00pm UTC](https://discourse.julialang.org/t/testing-different-constant-definitions/50376 "2020-11-18T18:00:06Z")

</div>

I have a macro, which creates / defines useful constants for the user. I would like to test if the macro created the correct constants and if they function as expected. How can I get something like the following to work? …

---

## [Function, constants, globals](https://discourse.julialang.org/t/function-constants-globals/48744)

<div class="topic-metadata">

**Author:** [@neo\_abs](https://discourse.julialang.org/u/neo_abs)\
**Replies:** 20\
**Last updated:** [October 23, 2020, 7:02pm UTC](https://discourse.julialang.org/t/function-constants-globals/48744 "2020-10-23T19:02:20Z")

</div>

I wanted to have a function: function test() const global x = 1 end Obviously it does not work. Is there a workaround that would make it feasible to declare constant globals from function? I want to have some sort…

---

## [Mutation of constants](https://discourse.julialang.org/t/mutation-of-constants/25894)

<div class="topic-metadata">

**Author:** [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Replies:** 6\
**Last updated:** [July 1, 2019, 3:43pm UTC](https://discourse.julialang.org/t/mutation-of-constants/25894 "2019-07-01T15:43:58Z")

</div>

As documented, why do we even allow these in the first place? Why and how would they be useful? Constant mutated to a different memory location without warning. julia\> const s1 = "1" "1" julia\> s2 = "1" "1" julia\> …

---

## [Constants in generic code](https://discourse.julialang.org/t/constants-in-generic-code/18920)

<div class="topic-metadata">

**Author:** [@Bernard\_GODARD](https://discourse.julialang.org/u/Bernard_GODARD)\
**Replies:** 1\
**Last updated:** [December 22, 2018, 1:36pm UTC](https://discourse.julialang.org/t/constants-in-generic-code/18920 "2018-12-22T13:36:32Z")

</div>

I am wondering if there is a recommended way to deal with constants in generic code (ie supporting user types). Example adding a rational f(x) = x+2/3 Implementation 1: f(x::T) where T = x + T(2)/T(3) Implementati…

---

## [Constant declaration in modules (SQLite)](https://discourse.julialang.org/t/constant-declaration-in-modules-sqlite/14801)

<div class="topic-metadata">

**Author:** [@moesphere](https://discourse.julialang.org/u/moesphere)\
**Replies:** 7\
**Last updated:** [September 11, 2018, 9:17am UTC](https://discourse.julialang.org/t/constant-declaration-in-modules-sqlite/14801 "2018-09-11T09:17:14Z")

</div>

Following minimal code worked fine with julia 0.6.4, but does not work with 1.0.0. Defining a minimal module: module TestModule using SQLite fp = "" const db = SQLite.DB(fp) end Loading the module and execute opera…

---

## [Correct way to declare global constant in modules](https://discourse.julialang.org/t/correct-way-to-declare-global-constant-in-modules/10234)

<div class="topic-metadata">

**Author:** [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Replies:** 15\
**Last updated:** [April 9, 2018, 2:17pm UTC](https://discourse.julialang.org/t/correct-way-to-declare-global-constant-in-modules/10234 "2018-04-09T14:17:42Z")

</div>

Hi guys, I have a module with a lot of global constants. Those are related to physical quantities, like Earth gravitational parameter. These are declared as: const R0 = 6378137.0 I was analyzing the type-stability of …

---

## [Does declaring a variable to be constant do anything at all?](https://discourse.julialang.org/t/does-declaring-a-variable-to-be-constant-do-anything-at-all/6029)

<div class="topic-metadata">

**Author:** [@tparker](https://discourse.julialang.org/u/tparker)\
**Replies:** 10\
**Last updated:** [September 21, 2017, 11:07pm UTC](https://discourse.julialang.org/t/does-declaring-a-variable-to-be-constant-do-anything-at-all/6029 "2017-09-21T23:07:07Z")

</div>

The code const x = 4 x = 3 issues a warning but still changes the value of x. And you can change bits-type fields of constant instances of mutable types without even getting a warning: mutable struct Mut x::Int64 en…
