# Comptime dependant variable

**URL:** <https://ziggit.dev/t/comptime-dependant-variable/7134>\
**Category:** Explain\
**Tags:** language\
**Created:** [December 2, 2024, 11:07pm UTC](https://ziggit.dev/t/comptime-dependant-variable/7134 "2024-12-02T23:07:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [December 2, 2024, 11:07pm UTC](https://ziggit.dev/t/comptime-dependant-variable/7134/1 "2024-12-02T23:07:10Z")

</div>

As far as I know this comptime function will be “splitted” in two dedicated functions.  
In reality my function is more complicated and longer and I cannot really move the local `value` inside the “false” version of this function, without a lot of copy paste.  
Normally you get a warning “unused variable” from the compiler but not here.

Will the local `value` in the “true” version of this function be eliminated from the stack? Disappear into nothingness?

```zig
fn do_something(comptime calc: bool) u16
{
    var value: u16 = 0;
    if (calc)
    {
        // calculate local value...
        return value; 
    }
    else 
    {
        return some_cached_value;
    }
}

```

---

<div class="post-metadata">

**Author:** ![mnemnion](https://ziggit.dev/user_avatar/ziggit.dev/mnemnion/32/2478_2.png) [@mnemnion](https://ziggit.dev/u/mnemnion)\
**Post date:** [December 2, 2024, 11:41pm UTC](https://ziggit.dev/t/comptime-dependant-variable/7134/2 "2024-12-02T23:41:12Z")

</div>

There’s only one way to be sure…

> **[Compiler Explorer - Zig (zig trunk)](https://godbolt.org/z/beW1EKdrT)**
>
> fn do\_something(comptime calc: bool, a: u16, b: u16, c: u16) u16
> {
> var value: u16 = 0;
> if (calc)
> {
> value = a \* b - c;
> return value; 
> }
> else 
> {
> return 42;
> }
> }
> 
> export fn something() c\_int {
> if...

Change `true` to `false` and see what you think.

---

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [December 3, 2024, 12:54am UTC](https://ziggit.dev/t/comptime-dependant-variable/7134/3 "2024-12-03T00:54:16Z")

</div>

As far as my asm knowledge goes: I think I see too much when `false`.  
A stackframe for the parameters?

---

<div class="post-metadata">

**Author:** ![mnemnion](https://ziggit.dev/user_avatar/ziggit.dev/mnemnion/32/2478_2.png) [@mnemnion](https://ziggit.dev/u/mnemnion)\
**Post date:** [December 3, 2024, 1:53am UTC](https://ziggit.dev/t/comptime-dependant-variable/7134/4 "2024-12-03T01:53:18Z")

</div>

It looks like it’s still getting called with the passed parameters, but just returns the cached value rather than doing anything with them. I’m not sure why that is.

---

<div class="post-metadata">

**Author:** ![Travis](https://ziggit.dev/letter_avatar_proxy/v4/letter/t/bbce88/32.png) [@Travis](https://ziggit.dev/u/Travis)\
**Post date:** [December 3, 2024, 2:54am UTC](https://ziggit.dev/t/comptime-dependant-variable/7134/5 "2024-12-03T02:54:49Z")

</div>

I think there are a some problems in that godbolt code.

First, -D=ReleaseFast doesn’t do anything. Surprised it isn’t rejected. With -OReleaseFast you’ll see that it compiles down to nothing.

I think this may be a better demo [https://godbolt.org/z/4YMvzbYcv](https://godbolt.org/z/4YMvzbYcv)

---

<div class="post-metadata">

**Author:** ![mnemnion](https://ziggit.dev/user_avatar/ziggit.dev/mnemnion/32/2478_2.png) [@mnemnion](https://ziggit.dev/u/mnemnion)\
**Post date:** [December 3, 2024, 4:49am UTC](https://ziggit.dev/t/comptime-dependant-variable/7134/6 "2024-12-03T04:49:31Z")

</div>

Yep, should have been `-Doptimize=ReleaseFast`. But that’s the `zig build` version of the flag and it’s using `zig build-obj`. Good catch.
