Skip to content

Wraparound +% not for literals? #964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
PavelVozenilek opened this issue Apr 28, 2018 · 3 comments
Closed

Wraparound +% not for literals? #964

PavelVozenilek opened this issue Apr 28, 2018 · 3 comments

Comments

@PavelVozenilek
Copy link

I try this in online tool:

const std = @import("std");
const warn = std.debug.warn;

pub fn main() void {

  const o : u8 = 200 +% 200;
  warn("{}\n", o);    
}

and it gives me error:

/home/runner/.code.tio:6:22: error: integer value 400 cannot be implicitly casted to type 'u8'

I expected wraparounds to work at compile time too.

@andrewrk
Copy link
Member

comptime integers never wrap. They have unlimited bits. You need to cast your literals to a specific number of bits before doing a wraparound operation.

@PavelVozenilek
Copy link
Author

Contrary to previous example, where intent was clear, this compiles:

pub fn main() void {

  const o = 200 +% 200;
}

IMHO it should not. It is not clear what the programmer wanted.

@PavelVozenilek
Copy link
Author

@andrewrk

This compiles and produces correct result:

const std = @import("std");
const warn = std.debug.warn;

pub fn main() void {

  const o : u8 = u8(200) +% u8(200);
  warn("{}\n", o);    
}

But it feels as superfluous, to cast everything when there is no ambiguity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants