Jump to page: 1 2 3
Thread overview
First Draft: cast(ref T)... as shorthand for *cast(T*)&...
5 days ago
Walter Bright
4 days ago
Walter Bright
4 days ago
Paul Backus
3 days ago
IchorDev
3 days ago
Walter Bright
2 days ago
Paul Backus
2 days ago
Andrea Fontana
2 days ago
ryuukk_
8 hours ago
Dom DiSc
1 day ago
Walter Bright
1 day ago
Paul Backus
1 day ago
Dom DiSc
3 days ago
IchorDev
3 days ago
Walter Bright
2 days ago
IchorDev
2 days ago
Walter Bright
1 day ago
IchorDev
3 days ago
Juraj
3 days ago
Walter Bright
2 days ago
IchorDev
2 days ago
Walter Bright
5 days ago
Proposed by Manu https://github.com/dlang/dmd/issues/20644

PR: https://github.com/dlang/dmd/pull/20728
4 days ago

On Saturday, 18 January 2025 at 07:31:40 UTC, Walter Bright wrote:

>

Proposed by Manu https://github.com/dlang/dmd/issues/20644

PR: https://github.com/dlang/dmd/pull/20728

Oh, I really like this one. I've done that reinterpret cast quite a bit in my code.

-Steve

4 days ago
On Saturday, 18 January 2025 at 07:31:40 UTC, Walter Bright wrote:
> Proposed by Manu https://github.com/dlang/dmd/issues/20644
>
> PR: https://github.com/dlang/dmd/pull/20728

Why not just add a simple `reinterpretCast!T` helper function to Phobos or druntime?
4 days ago
On 1/19/2025 8:14 AM, Steven Schveighoffer wrote:
> Oh, I really like this one. I've done that reinterpret cast quite a bit in my code.

Yeah, I wish I'd thought of it. Fortunately, Manu did!

3 days ago

On Sunday, 19 January 2025 at 17:33:33 UTC, Paul Backus wrote:

>

On Saturday, 18 January 2025 at 07:31:40 UTC, Walter Bright wrote:

>

Proposed by Manu https://github.com/dlang/dmd/issues/20644

PR: https://github.com/dlang/dmd/pull/20728

Why not just add a simple reinterpretCast!T helper function to Phobos or druntime?

Greater cost to compile time? Generally language features for commonly used patterns are pretty desirable.

3 days ago

On Saturday, 18 January 2025 at 07:31:40 UTC, Walter Bright wrote:

>

Proposed by Manu https://github.com/dlang/dmd/issues/20644

PR: https://github.com/dlang/dmd/pull/20728

I would really like an actual DIP that I can read.

First: I think this type of cast should be @safe where conservatively possible. For example, I can’t think of any reason that casting from int to float in this way shouldn’t be @safe. For structs it’s more complicated, but you could try to check if both are POD, make sure the size is equal or less, etc.
Reinterpretation into bool is obviously always unsafe. ;)

Second: Is there a way to make this syntax also simplify these 2 similar patterns?

T[] foo = (cast(T*)malloc(T.sizeof * n))[0..n];
void[] foo = (cast(void*)&bar)[0..bar.sizeof];

Third: This is a long shot, but should we make it so that casting to a larger type is @safe by populating the remaining space with zeroes?

short foo = 10;
auto baz = cast(ref int)foo; //remaining 2 bytes filled with zeroes

It’s different from cast(long) in that the remaining bytes are always filled with zero, and it would always be the trailing bytes that are zero (on big endian systems, proper zero-extension prepends the zeroed bytes, not appends):
LE sign extension via cast(int):
0A 00 -> 0A 00 00 00
BE sign extension via cast(int):
00 0A -> 00 00 00 0A
LE cast(ref int):
0A 00 -> 0A 00 00 00
BE cast(ref int):
00 0A -> 00 0A 00 00
I can see this being useful for esoteric byte order manipulation. Moreso it’s actually useful for casting between struct types that have ‘inheritance’.

3 days ago

On Saturday, 18 January 2025 at 07:31:40 UTC, Walter Bright wrote:

>

Proposed by Manu https://github.com/dlang/dmd/issues/20644

PR: https://github.com/dlang/dmd/pull/20728

This is how an unaware reader will parse the code:

ubyte val = 123;
auto x = cast(const long) val;     // x is const
auto y = cast(immutable long) val; // y is immutable
auto z = cast(shared long) val;    // z is shared

auto w = cast(ref long) val;       // w is ref

We now have local ref, and that makes this syntax a cognitive burden.

I know, that the type is const(long) and there is no ref(long) so this is technically correct, still, it will confuse users for a very little benefit.
Reinterpret cast is not a "commonly used patterns" it is a necessary evil, one can always make a local helper for it, if importing a module is a compile time issue.

Another example:

auto c0 = cast(scope long) val;   // Error
auto c1 = cast(ref long) val;     // Compiles, but confusing.

I am a sucker for syntax sugar, but this one, imho, sucks.

Juraj

3 days ago
On 1/19/2025 9:33 AM, Paul Backus wrote:
> Why not just add a simple `reinterpretCast!T` helper function to Phobos or druntime?

Great question.

`reinterpretcast!T` is just ugly. I never liked it in C++. `cast(ref T)`, on the other hand, looks nice.

We've seen other aesthetic improvements in D, such as:
```
int square(int x) { return x * x; }
```
becomes:
```
int square(int x) => x * x;
```
and other shortened syntax for lambdas, and the `if` declaration variant.
3 days ago
On 1/20/2025 1:07 AM, IchorDev wrote:
> On Saturday, 18 January 2025 at 07:31:40 UTC, Walter Bright wrote:
>> Proposed by Manu https://github.com/dlang/dmd/issues/20644
>>
>> PR: https://github.com/dlang/dmd/pull/20728
> 
> I would really like an actual DIP that I can read.

It wouldn't be any more than what I wrote in:

https://github.com/dlang/dlang.org/pull/4164


> First: I think this type of cast should be `@safe` where conservatively possible. For example, I can’t think of any reason that casting from `int` to `float` in this way shouldn’t be `@safe`. For structs it’s more complicated, but you could try to check if both are POD, make sure the size is equal or less, etc.
> Reinterpretation into `bool` is obviously always unsafe. ;)

That's a good idea for a future enhancement.

> Second: Is there a way to make this syntax also simplify these 2 similar patterns?
> ```d
> T[] foo = (cast(T*)malloc(T.sizeof * n))[0..n];
> ```

A template?

> ```d
> void[] foo = (cast(void*)&bar)[0..bar.sizeof];
> ```

I haven't really thought about that. But this also works and is simpler and easier to read:

```
void[] foo = cast(void[])((&bar)[0 .. 1]);
```


> Third: This is a long shot, but should we make it so that casting to a larger type is `@safe` by populating the remaining space with zeroes?
> ```d
> short foo = 10;
> auto baz = cast(ref int)foo; //remaining 2 bytes filled with zeroes
> ```

That wouldn't be good because it would stomp on whatever is next in memory.

3 days ago
On 1/20/2025 2:07 AM, Juraj wrote:
> We now have local `ref`, and that makes this syntax a cognitive burden.

Think of `cast(ref int)f` as "refer to `f` as if it were an `int`" and it will make perfect sense.
« First   ‹ Prev
1 2 3