( Sorry for this odd image, but as new user can only upload one pic. )
The documentation says Block are expressions
, so it should return the last evaluated value?
The documentation says Block are expressions
, so it should return the last evaluated value?
Welcome to Ziggit!
Yes, blocks can be used as expressions, but only with labels and an explicit break. Here’s a relevant proposal which was rejected by Andrew: Restore block expressions · Issue #9758 · ziglang/zig · GitHub.
I’ve already considered (and implemented) block expressions, and then removed them from the language, and I also reconsidered adding them back when reading the comments in this issue. Ultimately a decision needs to be made. And here it is.
An upside of this is explicitness, which Zig tries to emphasize. You can instantly see where the value is going based on the label used with the break
.
PS: It’s better if you post code/errors in text format rather than images, because it allows people to copy the code without typing it out. You can make a code block surrounded by triple backticks (`) like so:
```
// Code…
```
This displays as:
// Code...
Thanks for quoting the Issue, really good discussions out there.
// snippet-1
const x={ 10; }
// snippet-2
{
10;
}
Both code snippets above got the same error.
error: value of type 'comptime_int' ignored
What does it really mean?
In Zig, whenever you write an expression/value, you have to use it, i.e assign it to a variable, use it as a function argument, etc. The only exception to this is if the expression is of type void
.
const std = @import("std")
pub fn main !void {
foo(); // the return value can be ignored, as it is void
bar(); // the return value must be used in some way
}
fn foo() void {}
fn bar() i32 {
return 80;
}
If you want to ignore a value, you need to explicitly discard it:
_ = bar(); // '_' is a special identifier for discarding values
So the error means that the value (10) which is a comptime_int
(compile-time known integer) is ignored because it is not actually being assigned to x or used in any way.
Really elaborate. Thanks.