openlang

Binding a function’s void result?

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?

The conversation continues 4

Follow the reasoning
  1. Permalink

    It simplifies code in which comptime decides whether there is a value at all.

    Rather than picking apart a function's type, potentially through several error unions, checking the type of its bound result is much simpler, and thats useful here.

    You may also need the call to happen anyway because it performs other work.

    Allowing a value with no other use doesn't cause much trouble.

  2. Permalink

    I'm wondering which items need “discard”-ing inside a test block, and when? As an example, the end of the std.zig module's source contains this:

    test {
        _ = Ast;
        _ = AstRlAnnotate;
        _ = BuiltinFn;
        _ = Client;
        _ = ErrorBundle;
        _ = LibCDirs;
        _ = LibCInstallation;
        _ = Server;
        _ = TokenSmith;
        _ = WindowsSdk;
        _ = number_literal;
        _ = primitives;
        _ = string_literal;
        _ = system;
        _ = target;
        _ = c_translation;
        _ = llvm;
        _ = @import("zig/parser_fuzz.zig");
    }

    For a library, should every module I import go there? Otherwise, which modules need this treatment?

  3. Permalink

    For a library, should every module I import go there? Otherwise, which modules need this treatment?

    Tests run implicitly for the root file alone, so with other files/modules/namespaces, the namespace containing those tests must be referenced. That also applies to a struct/union/enum containing tests inside the root file, each needs a reference as well.

    For clarity, mentioning sample.member references only sample.member, not sample, while _ = sample references sample itself.

Other sides of the same question

  1. How can I subclass a class inside another class?

    Hi, In a package whose code is outside my control, there is a hierarchy of classes arranged as follows: I'm trying to add a w…

    Replies 1Latest activity scala
  2. Comparing have with let bindings in Lean4

    I've observed that these two expressions and behave identically. This also happens in theorem proofs. Are they interchangeabl…

    Replies 3Latest activity lean-4
  3. TypeScript Map initialization with string array entries

    I'm creating a Map whose entries each hold an array of strings. How can I declare its type and initialize it together in one…

    Replies 2Latest activity typescript