August 29, 2021
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?


August 29, 2021
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 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;
}

void main() {
  foo(42, 42);    // Works
  foo(100, 100);  // Throws ConvOverflowException
}

Of course, because of the checks, 'to' is slower than casting blindly.

Ali

August 29, 2021
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 this zombie-ish thread. :)

Ali
August 29, 2021

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;
}

void main() {
  foo(42, 42);    // Works
  foo(100, 100);  // Throws ConvOverflowException
}

I was going to suggest std.experimental.checkedint as an alternative here, but it turns out that it does integer promotion too--Checked!byte + Checked!byte == Checked!int.

This seems obviously wrong to me, but according to run.dlang.io it's always worked that way.

August 29, 2021

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) {
  import std.conv : to;
  return (a + b).to!byte;
}

void main() {
  foo(42, 42);    // Works
  foo(100, 100);  // Throws ConvOverflowException
}

I was going to suggest std.experimental.checkedint as an alternative here, but it turns out that it does integer promotion too--Checked!byte + Checked!byte == Checked!int.

This seems obviously wrong to me, but according to run.dlang.io it's always worked that way.

(a.checked + b).get is int, but this works:

import std.experimental.checkedint;

byte foo(byte a, byte b) {
    auto c = a.checked;
    c += b;
    return c.get;
}

unittest {
    import std.exception : assertThrown;
    import core.exception : AssertError;

    foo(42, 42);
    assertThrown!AssertError(foo(100, 100));
}

... after Phobos is patched.

error: undefined identifier ‘Lhs’, did you mean alias ‘Rhs’?
August 29, 2021

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, and as far as I can tell has never even been reported before.

Bugzilla: https://issues.dlang.org/show_bug.cgi?id=22249

1 2
Next ›   Last »