Skip to content

if expression doesn't coerce to slice in anonymous struct #7197

Closed
@mkeeter

Description

@mkeeter
const std = @import("std");

pub fn main() !void {
    // Read from user input to convince compiler to not optimize out conditional
    const stdin = std.io.getStdIn().reader();
    var buf: [10]u8 = undefined;
    const n = try stdin.read(&buf);

    const f = if (n % 2 == 0) "hi" else "world"; // works
    const g = .{if (n % 2 == 0) "hi" else "world"}; // doesn't work
    std.debug.print("{}, {}\n", .{ f, g });
}

f correctly recognizes that it should be a []const u8, but that coercion doesn't happen inside of the anonymous struct g, which is unexpected!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions