Re: byte + byte = int: why?

2021-08-29 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 29 August 2021 at 16:21:40 UTC, jfondren wrote: ... after Phobos is patched. ``` error: undefined identifier ‘Lhs’, did you mean alias ‘Rhs’? ``` Shows how much anyone actually uses this code, I guess--the bug was introduced [in 2017][1], and as far as I can tell has never even

Re: byte + byte = int: why?

2021-08-29 Thread jfondren via Digitalmars-d-learn
On Sunday, 29 August 2021 at 15:57:18 UTC, Paul Backus wrote: On Sunday, 29 August 2021 at 15:42:18 UTC, Ali Çehreli wrote: Depending on the situation, you may want to use std.conv.to, which does a value range check and throws an exception to prevent an error: byte foo(byte a, byte b) {

Re: byte + byte = int: why?

2021-08-29 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 29 August 2021 at 15:42:18 UTC, Ali Çehreli wrote: Depending on the situation, you may want to use std.conv.to, which does a value range check and throws an exception to prevent an error: byte foo(byte a, byte b) { import std.conv : to; return (a + b).to!byte; }

Re: byte + byte = int: why?

2021-08-29 Thread Ali Çehreli via Digitalmars-d-learn
On 1/18/19 9:09 AM, Mek101 wrote: > source/hash.d(11,25): Error: cannot implicitly convert expression > `cast(int)temp[fowardI] + cast(int)temp[backwardI]` of type `int` to `byte` Others suggested casting as a solution which works but it will hide potential errors. Depending on the

Re: byte + byte = int: why?

2021-08-29 Thread Ali Çehreli via Digitalmars-d-learn
On 8/29/21 3:57 AM, Rekel wrote: On Friday, 18 January 2019 at 18:49:23 UTC, Steven Schveighoffer wrote: As others have said, those are the rules D has for historical reasons, you just have to deal with them. Is that to ensure compatibility with C? Yes. It's mentioned multiple times in

Re: byte + byte = int: why?

2021-08-29 Thread Rekel via Digitalmars-d-learn
On Friday, 18 January 2019 at 18:49:23 UTC, Steven Schveighoffer wrote: As others have said, those are the rules D has for historical reasons, you just have to deal with them. Is that to ensure compatibility with C?

Re: byte + byte = int: why?

2019-01-18 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jan 18, 2019 at 01:49:23PM -0500, Steven Schveighoffer via Digitalmars-d-learn wrote: > On 1/18/19 12:26 PM, Mek101 wrote: [...] > > Then why isn't int + int = long? If i did the example above, they > > wouldn't fit in a int for sure, yet the result is of the same type. > > Why should

Re: byte + byte = int: why?

2019-01-18 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/18/19 12:26 PM, Mek101 wrote: On Friday, 18 January 2019 at 17:15:09 UTC, Steven Schveighoffer wrote: What is 127 + 127? Answer: 254. Which if converted to a byte is -127. Not what you might expect if you are doing addition. Quite similar to int.max + int.max Indeed. But this is where

Re: byte + byte = int: why?

2019-01-18 Thread Adam D. Ruppe via Digitalmars-d-learn
On Friday, 18 January 2019 at 17:26:35 UTC, Mek101 wrote: Then why isn't int + int = long? If i did the example above, they wouldn't fit in a int for sure, yet the result is of the same type. Why should byte be any different? Because C didn't define it that way. And C didn't define it that

Re: byte + byte = int: why?

2019-01-18 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jan 18, 2019 at 05:26:35PM +, Mek101 via Digitalmars-d-learn wrote: > On Friday, 18 January 2019 at 17:15:09 UTC, Steven Schveighoffer wrote: > > What is 127 + 127? Answer: 254. Which if converted to a byte is > > -127. Not what you might expect if you are doing addition. > > Quite

Re: byte + byte = int: why?

2019-01-18 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jan 18, 2019 at 05:09:52PM +, Mek101 via Digitalmars-d-learn wrote: > I have the following line of code: > > temp[fowardI] = temp[fowardI] + temp[backwardI]; > > Where temp is a byte array (byte[]). When I try to compile it dmd > gives me this error: > >

Re: byte + byte = int: why?

2019-01-18 Thread Adam D. Ruppe via Digitalmars-d-learn
On Friday, 18 January 2019 at 17:09:52 UTC, Mek101 wrote: Where temp is a byte array (byte[]) Oh btw, D's byte is signed, range -128 to 127, unlike C# where it is unsigned. C#'s `byte` is represented as D's `ubyte` (meaning "unsigned byte"), range 0-255.

Re: byte + byte = int: why?

2019-01-18 Thread Mek101 via Digitalmars-d-learn
On Friday, 18 January 2019 at 17:15:09 UTC, Steven Schveighoffer wrote: What is 127 + 127? Answer: 254. Which if converted to a byte is -127. Not what you might expect if you are doing addition. Quite similar to int.max + int.max In fact, D promotes all integral types smaller than int to int

Re: byte + byte = int: why?

2019-01-18 Thread Adam D. Ruppe via Digitalmars-d-learn
On Friday, 18 January 2019 at 17:09:52 UTC, Mek101 wrote: Meaning that the byte type doesn't have a + operator. Well, it DOES have a + operator, it just returns int that can be squished to a size if and only if the compiler proves it fits. A bit of background: the C language was designed on

Re: byte + byte = int: why?

2019-01-18 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/18/19 12:09 PM, Mek101 wrote: I have the following line of code:     temp[fowardI] = temp[fowardI] + temp[backwardI]; Where temp is a byte array (byte[]). When I try to compile it dmd gives me this error:     source/hash.d(11,25): Error: cannot implicitly convert expression