# What do you think about using generated functions for constants?

**URL:** https://discourse.julialang.org/t/what-do-you-think-about-using-generated-functions-for-constants/87149
**Category:** General Usage
**Created:** [September 12, 2022, 4:16pm UTC](https://discourse.julialang.org/t/what-do-you-think-about-using-generated-functions-for-constants/87149 "2022-09-12T16:16:52Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)
#### Post date: [September 12, 2022, 4:16pm UTC](https://discourse.julialang.org/t/what-do-you-think-about-using-generated-functions-for-constants/87149/1 "2022-09-12T16:16:52Z")

</div>

Hellow, what do you think about using generated functions for constants like this?

`@generated get_const() = :($([i for i = 1:100_000_000])) # constant is evaluated when called`

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [September 12, 2022, 4:50pm UTC](https://discourse.julialang.org/t/what-do-you-think-about-using-generated-functions-for-constants/87149/2 "2022-09-12T16:50:07Z")

</div>

There is a pretty involved discussion about this topic here: [Constant propagation vs generated functions](https://discourse.julialang.org/t/constant-propagation-vs-generated-functions/83788)

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [September 12, 2022, 5:10pm UTC](https://discourse.julialang.org/t/what-do-you-think-about-using-generated-functions-for-constants/87149/3 "2022-09-12T17:10:24Z")

</div>

You don’t need `@generated` for this, just `@eval`.

---

<div class="post-metadata">

### Author: ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)
#### Post date: [September 12, 2022, 5:35pm UTC](https://discourse.julialang.org/t/what-do-you-think-about-using-generated-functions-for-constants/87149/4 "2022-09-12T17:35:16Z")

</div>

I can imagine two behaviors you might want for a function like this:

1. Allocate once, and reuse always point to the same memory
2. Allocate at each call to `get_const()`, creating a new memory each time

I think the standard ways to do this would be something like

```julia
const myconst = collect(1:100_000_000)

get_const1() = myconst

```

and

```julia
get_const2() = collect(1:100_000_000)

```

The generated function approach for this use case has a couple of problems. First, It’s not immediately clear which behavior it will have. Testing it shows that it behaves like (1) above, though there are no guarantees! For generated functions, the compiler can choose when to regenerate the code.

Generated functions are also harder for most users to read, so you should use them only when there’s a clear advantage over a more idiomatic approach.
