Zig: Proposal: Make unary minus for unsigned types a compile error

Created on 11 Jun 2020  路  4Comments  路  Source: ziglang/zig

fn negate(x: u32) u32 {
    return -x;
}

This is currently safety-checked undefined behaviour, however, this misstake can already be catched
at compile time.

accepted proposal

Most helpful comment

Compiler error makes more sense IMO. If you want to change signed <-> unsigned, you should have to use an explicit cast.

All 4 comments

Another option is to make -unsigned return a signed type of higher bitness, so

fn negate(x: u32) i33 {
  return -x;
}

would be valid.

Compiler error makes more sense IMO. If you want to change signed <-> unsigned, you should have to use an explicit cast.

Related: #1770

Closed by #5654

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dobkeratops picture dobkeratops  路  3Comments

DavidYKay picture DavidYKay  路  3Comments

andrewrk picture andrewrk  路  3Comments

jayschwa picture jayschwa  路  3Comments

zimmi picture zimmi  路  3Comments