#Is "comptime" necessary here?

1 messages · Page 1 of 1 (latest)

split temple
#

In the zig documentation found I this example.

const expect = @import("std").testing.expect;

test "optional type" {
    // Declare an optional and coerce from null:
    var foo: ?i32 = null;

    // Coerce from child type of an optional
    foo = 1234;

    // Use compile-time reflection to access the child type of the optional:
    try comptime expect(@typeInfo(@TypeOf(foo)).Optional.child == i32);
}

What's the significance of comptime here? The test ran successfully, even when I removed the comptime keyword here.

slate bear
#

I don't think there's any real significance semantically

#

all it really does is make it so that the expect(...) call is evaluated at compile time

#

meaning that the code is essentially equivalent to {}

#

and if it were changed to != i32

#

it would be essentially equivalent to if (true) return error.TestUnexpectedResult;

#

without the comptime, it's just equivalent to try expect(true);

split temple
#

@slate bear Thanks for pointing out to change the condition to != i32. I can see the two error messages are slightly different.