简体   繁体   中英

struct definition with var instead of const in zig language

I am now learning zig language. I have seen definitions of structs with const keyword like

const X = struct {
    n: i32,
};

My understanding is that const is a kind of complementary to var , the latter allows change, the former does not. But what would mean defining struct with var ?

var Y = struct {
    n: i32,
};

Is this legal? I compiles, so yes, it is. But what is the meaning and use of this?

That compiles because zig is lazy evaluated. Because Y is not used, the compiler doesn't check it.

When you reference it, the compiler throw an error:

Try this code

var Y = struct {
    n: i32,
};

comptime {
    @compileLog(Y);
}
error: variable of type 'type' must be constant
    var Y = struct {
    ^

var is to declare variables. When you use var in a global scope, that creates a global variable.

In your case,

var Y = struct {
    n: i32,
};

Declares Y as a variable of infered type. In this case, Y is a variable of type type .

In zig, there is comptime-only types, that is the case of type . A value where the type is comptime-only type only can live inside the compiler, you can't create the value in runtime 1 . So, the compiler needs always to comptime-known the value.

So, because Y is a global variable. You can modify it in runtime. That is the reason for the error. The value of Y cannot be generated/stored by the binary.


If only lives in the compiler, works

Try this code

comptime {
    var Y = struct {
        n: i32,
    };

    Y = struct {
        count: u32,
    };

    const concrete = Y { .count = 10 };

    @compileLog(concrete.count);
}
| 10 

Appendix

1 For example, consider

Try this code

const std = @import("std");

fn compilerKnown(arg: []const u8) type {
   return u64;
}

pub fn main() !void {
    var runtimeValue = "hello world";

    std.debug.print("{}\n", .{ compilerKnown(runtimeValue) });
}
error: unable to evaluate constant expression
    std.debug.print("{}\n", .{ compilerKnown(runtimeValue) });
                                             ^

This is an error because zig try to compile the function compilerKnown into the binary, but the type type is comptime-only, so is unable to generate the binary. In particular, cannot generate the machine code for return u64 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM