Zig: Proposal: Make std.meta.Int accept a signedness parameter

Created on 16 Oct 2020  路  1Comment  路  Source: ziglang/zig

Currently, Zig's std.meta.Int function looks like this.
I propose the following change:

const Signedness = enum {
    u,
    i,
};

pub fn Int(comptime signedness: Signedness, comptime bit_count: u16) type {
    return @Type(TypeInfo{
        .Int = .{
            .is_signed = signedness == .i,
            .bits = bit_count,
        },
    });
}

Compare the following calls:

std.meta.Int(true, 32);
std.meta.Int(.i, 32);

std.meta.Int(false, 16);
std.meta.Int(.u, 16);

I think the proposed version looks much cleaner and closer to how one would normally write an integer type.
Note that this doesn't (and doesn't attempt to) change the TypeInfo representation of an integer and only changes the convenience std.meta API.

accepted breaking contributor friendly proposal standard library

Most helpful comment

Yes, please! I only want one change: .unsigned and .signed

>All comments

Yes, please! I only want one change: .unsigned and .signed

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jorangreef picture jorangreef  路  3Comments

bronze1man picture bronze1man  路  3Comments

DavidYKay picture DavidYKay  路  3Comments

fengb picture fengb  路  3Comments

dobkeratops picture dobkeratops  路  3Comments