Re: Question about ubyte x overflow, any safe way?

2019-08-05 Thread Patrick Schluter via Digitalmars-d-learn
On Monday, 5 August 2019 at 18:21:36 UTC, matheus wrote: On Monday, 5 August 2019 at 01:41:06 UTC, Ali Çehreli wrote: ... Two examples with foreach and ranges. The 'ubyte.max + 1' expression is int. The compiler casts to ubyte (because we typed ubyte) in the foreach and we cast to ubyte in

Re: Question about ubyte x overflow, any safe way?

2019-08-05 Thread matheus via Digitalmars-d-learn
On Monday, 5 August 2019 at 01:41:06 UTC, Ali Çehreli wrote: ... Two examples with foreach and ranges. The 'ubyte.max + 1' expression is int. The compiler casts to ubyte (because we typed ubyte) in the foreach and we cast to ubyte in the range: ... Maybe it was a bad example of my part

Re: Question about ubyte x overflow, any safe way?

2019-08-04 Thread Ali Çehreli via Digitalmars-d-learn
On 08/04/2019 11:12 AM, matheus wrote: Hi, The snippet below will produce an "infinite loop" because obviously "ubyte u" will overflow after 255: import std.stdio; void main(){     ubyte u = 250;     for(;u<256;++u){     writeln(u);     } } Question: Is there a way (Flag) to prevent

Re: Question about ubyte x overflow, any safe way?

2019-08-04 Thread matheus via Digitalmars-d-learn
On Sunday, 4 August 2019 at 18:38:34 UTC, Paul Backus wrote: ... Use std.experimental.checkedint: import std.stdio; import std.experimental.checkedint; void main() { for(Checked!(ubyte, Throw) u = ubyte(250); u < 256; ++u) { writeln(u.get); } } An exception will be thrown when

Re: Question about ubyte x overflow, any safe way?

2019-08-04 Thread Max Haughton via Digitalmars-d-learn
On Sunday, 4 August 2019 at 18:22:30 UTC, matheus wrote: On Sunday, 4 August 2019 at 18:15:30 UTC, Max Haughton wrote: What do you want to do? If you just want to count to 255 then use a foreach This was just an example, what I'd like in this code is either: Get an error (exception) when

Re: Question about ubyte x overflow, any safe way?

2019-08-04 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 4 August 2019 at 18:22:30 UTC, matheus wrote: On Sunday, 4 August 2019 at 18:15:30 UTC, Max Haughton wrote: What do you want to do? If you just want to count to 255 then use a foreach This was just an example, what I'd like in this code is either: Get an error (exception) when

Re: Question about ubyte x overflow, any safe way?

2019-08-04 Thread matheus via Digitalmars-d-learn
On Sunday, 4 August 2019 at 18:15:30 UTC, Max Haughton wrote: What do you want to do? If you just want to count to 255 then use a foreach This was just an example, what I'd like in this code is either: Get an error (exception) when overflow or even an warning (Only if "some" flag was

Re: Question about ubyte x overflow, any safe way?

2019-08-04 Thread Max Haughton via Digitalmars-d-learn
On Sunday, 4 August 2019 at 18:12:48 UTC, matheus wrote: Hi, The snippet below will produce an "infinite loop" because obviously "ubyte u" will overflow after 255: import std.stdio; void main(){ ubyte u = 250; for(;u<256;++u){ writeln(u); } } Question: Is there a way

Question about ubyte x overflow, any safe way?

2019-08-04 Thread matheus via Digitalmars-d-learn
Hi, The snippet below will produce an "infinite loop" because obviously "ubyte u" will overflow after 255: import std.stdio; void main(){ ubyte u = 250; for(;u<256;++u){ writeln(u); } } Question: Is there a way (Flag) to prevent this? Matheus.