이게 어떻게 통과하나요?
const std = @import("std");
pub fn main() !void {
const outcome =
std.debug.print("Hello, world\n", .{});
_ = outcome;
}
std.debug.print()의 결과는 void인데, 그 결과를 outcome에 넣어도 Zig는 아무 말도 하지 않네요.
프로젝트를 하다가 외부 라이브러리의 API가 값을 반환한다고 착각해서 이 문제를 만났어요. 결국 {s}로 그 결과를 문자열에 포맷하려니 컴파일러 오류가 났지만, 처음의 잘못된 가정까지 거슬러 올라가는 데 시간이 걸렸어요.
void를 변수에 저장하는 게 유용한 경우도 있나요?
영어에서 번역·원문 보기How is this accepted?
const std = @import("std");
pub fn main() !void {
const outcome =
std.debug.print("Hello, world\n", .{});
_ = outcome;
}
The result of std.debug.print() is void, yet binding that result to outcome produces no complaint from Zig.
I encountered this while working on a project, after incorrectly thinking an API from a third-party library produced a value when it actually didn't. Eventually, formatting that result as a string using {s} caused a compiler error, though tracing it back to my initial assumption took some time.
When would storing void in a variable be helpful?