# Why can't I provide default values for tagged union?

**URL:** <https://ziggit.dev/t/why-cant-i-provide-default-values-for-tagged-union/2804>\
**Category:** Help\
**Tags:** language\
**Created:** [January 9, 2024, 9:49am UTC](https://ziggit.dev/t/why-cant-i-provide-default-values-for-tagged-union/2804 "2024-01-09T09:49:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![timfayz](https://ziggit.dev/user_avatar/ziggit.dev/timfayz/32/1827_2.png) [@timfayz](https://ziggit.dev/u/timfayz)\
**Post date:** [January 9, 2024, 9:49am UTC](https://ziggit.dev/t/why-cant-i-provide-default-values-for-tagged-union/2804/1 "2024-01-09T09:49:44Z")

</div>

Recently, I wanted to specify default values for a tagged union but got some strange error messages:

```zig
const Foo = struct {
    bar: union(enum) {
        f1: ?bool = null,
        f2: ?bool = null,
    },
};

```

Throws:

```zig
howtos/q_taggedUnionDefVal.zig:4:10: error: explicitly valued tagged union missing integer tag type
    bar: union(enum) {
         ^ ~~~~
howtos/q_taggedUnionDefVal.zig:5:21: note: tag value specified here
        f1: ?bool = null,
                    ^~~~

```

Changing `union(enum)` to `enum(u1)` or to a separately defined enum throws something contradictory:

```zig
error: explicitly valued tagged union requires inferred enum tag type

```

I found an open issue, [Incorrect error when assigning default value to tagged union in struct · Issue #14561 · ziglang/zig · GitHub](https://github.com/ziglang/zig/issues/14561), that reports the error message is indeed confusing. I thought this is a compiler bug, but after I looked closely, the issue says that it is just the error message that is incorrect, not the behavior of the compiler itself, and it should have reported something like “error: unions do not support default values” instead. Are default values for tagged unions not going to be supported? If so, why?

---

<div class="post-metadata">

**Author:** ![IntegratedQuantum](https://ziggit.dev/user_avatar/ziggit.dev/integratedquantum/32/782_2.png) [@IntegratedQuantum](https://ziggit.dev/u/IntegratedQuantum)\
**Post date:** [January 9, 2024, 10:16am UTC](https://ziggit.dev/t/why-cant-i-provide-default-values-for-tagged-union/2804/2 "2024-01-09T10:16:55Z")

</div>

How would you expect default union fields to work? For structs it’s simple, you just do `.{}` and all fields get initialized, this makes intuitive sense to write and is easy for the compiler to parse.  
But for unions we need a tag field `Union{.tag = value}`. Should a new syntax be introduced for a feature like this that almost no one will actually use?

---

<div class="post-metadata">

**Author:** ![timfayz](https://ziggit.dev/user_avatar/ziggit.dev/timfayz/32/1827_2.png) [@timfayz](https://ziggit.dev/u/timfayz)\
**Post date:** [January 9, 2024, 10:17am UTC](https://ziggit.dev/t/why-cant-i-provide-default-values-for-tagged-union/2804/3 "2024-01-09T10:17:23Z")

</div>

Ok, I think I asked a bit silly question 😊. I can’t because there is no way to decide which field is going to be initialized. So what I needed is just to move the defaults in the right place:

```zig
const Foo = struct {
    bar: union(u1) {
        f1: ?bool,
        f2: ?bool,
    } = .{ .f1 = true },
};

```
