# How to use StructField.default\_value

**URL:** <https://ziggit.dev/t/how-to-use-structfield-default-value/4276>\
**Category:** Help\
**Tags:** language\
**Created:** [May 11, 2024, 11:02am UTC](https://ziggit.dev/t/how-to-use-structfield-default-value/4276 "2024-05-11T11:02:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![n0s4](https://ziggit.dev/user_avatar/ziggit.dev/n0s4/32/2227_2.png) [@n0s4](https://ziggit.dev/u/n0s4)\
**Post date:** [May 11, 2024, 11:02am UTC](https://ziggit.dev/t/how-to-use-structfield-default-value/4276/1 "2024-05-11T11:02:54Z")

</div>

I’m making a function to generate a default value of any struct type at comptime, it should use the struct’s default values if present, but set any bool fields to false and optional fields to null. Here’s my code:

```zig
 fn makeConfig(comptime Config: type) Config {
    var config: Config = undefined;
    if (@typeInfo(Config) != .Struct) @compileError("Config must be a struct type.");

    inline for (@typeInfo(Config).Struct.fields) |field| {
        if (field.default_value) |default| {
            // Problem line:
            @field(config, field.name) = @as(field.type, default.*);
            continue;
        }

        switch (@typeInfo(field.type)) {
            .Bool => @field(config, field.name) = false,
            .Optional => @field(config, field.name) = null,
            else => @compileError("cannot generate default"),
        }
    }

    return config;
}

```

The problem is that on the line where it sets the value to the existing default, I get a compile error: `cannot load opaque type 'anyopaque'`. I understand that the default value is type erased, but can I not cast it to the type of the field? Using `@as(field.type, default.*)` does not work either.

---

<div class="post-metadata">

**Author:** ![dude\_the\_builder](https://ziggit.dev/user_avatar/ziggit.dev/dude_the_builder/32/557_2.png) [@dude\_the\_builder](https://ziggit.dev/u/dude_the_builder)\
**Post date:** [May 11, 2024, 11:19am UTC](https://ziggit.dev/t/how-to-use-structfield-default-value/4276/2 "2024-05-11T11:19:23Z")

</div>

Since it’s a pointer to an `anyopaque` I think you need a pointer cast?

```zig
@as(*const field.type, @ptrCast(default)).*

```

---

<div class="post-metadata">

**Author:** ![n0s4](https://ziggit.dev/user_avatar/ziggit.dev/n0s4/32/2227_2.png) [@n0s4](https://ziggit.dev/u/n0s4)\
**Post date:** [May 11, 2024, 11:01pm UTC](https://ziggit.dev/t/how-to-use-structfield-default-value/4276/3 "2024-05-11T23:01:47Z")

</div>

Ok, this almost works but I also need to `@alignCast`:

```zig
@field(config, field.name) = @as(*const field.type, @alignCast(@ptrCast(default))).*;

```

Thanks :).

---

<div class="post-metadata">

**Author:** ![Eisenhauer](https://ziggit.dev/letter_avatar_proxy/v4/letter/e/76d3ee/32.png) [@Eisenhauer](https://ziggit.dev/u/Eisenhauer)\
**Post date:** [May 12, 2024, 7:47am UTC](https://ziggit.dev/t/how-to-use-structfield-default-value/4276/4 "2024-05-12T07:47:51Z")

</div>

Just in case. It seems to me you are trying to reimplement [`std.mem.zeroInit`](https://ziglang.org/documentation/master/std/#std.mem.zeroInit).

---

<div class="post-metadata">

**Author:** ![n0s4](https://ziggit.dev/user_avatar/ziggit.dev/n0s4/32/2227_2.png) [@n0s4](https://ziggit.dev/u/n0s4)\
**Post date:** [May 13, 2024, 8:23pm UTC](https://ziggit.dev/t/how-to-use-structfield-default-value/4276/5 "2024-05-13T20:23:59Z")

</div>

I was not aware, thanks for sharing!
