On Saturday, 18 January 2025 at 07:31:40 UTC, Walter Bright wrote:
>Proposed by Manu https://github.com/dlang/dmd/issues/20644
Oh, I really like this one. I've done that reinterpret cast quite a bit in my code.
-Steve
Thread overview | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
5 days ago First Draft: cast(ref T)... as shorthand for *cast(T*)&... | ||||
---|---|---|---|---|
| ||||
Proposed by Manu https://github.com/dlang/dmd/issues/20644 PR: https://github.com/dlang/dmd/pull/20728 |
4 days ago Re: First Draft: cast(ref T)... as shorthand for *cast(T*)&... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On Saturday, 18 January 2025 at 07:31:40 UTC, Walter Bright wrote: >Proposed by Manu https://github.com/dlang/dmd/issues/20644 Oh, I really like this one. I've done that reinterpret cast quite a bit in my code. -Steve |
4 days ago Re: First Draft: cast(ref T)... as shorthand for *cast(T*)&... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | 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 Re: First Draft: cast(ref T)... as shorthand for *cast(T*)&... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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 Re: First Draft: cast(ref T)... as shorthand for *cast(T*)&... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | 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 Why not just add a simple Greater cost to compile time? Generally language features for commonly used patterns are pretty desirable. |
3 days ago Re: First Draft: cast(ref T)... as shorthand for *cast(T*)&... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On Saturday, 18 January 2025 at 07:31:40 UTC, Walter Bright wrote: >Proposed by Manu https://github.com/dlang/dmd/issues/20644 I would really like an actual DIP that I can read. First: I think this type of cast should be Second: Is there a way to make this syntax also simplify these 2 similar patterns?
Third: This is a long shot, but should we make it so that casting to a larger type is
It’s different from |
3 days ago Re: First Draft: cast(ref T)... as shorthand for *cast(T*)&... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On Saturday, 18 January 2025 at 07:31:40 UTC, Walter Bright wrote: >Proposed by Manu https://github.com/dlang/dmd/issues/20644 This is how an unaware reader will parse the code:
We now have local I know, that the type is Another example:
I am a sucker for syntax sugar, but this one, imho, sucks. Juraj |
3 days ago Re: First Draft: cast(ref T)... as shorthand for *cast(T*)&... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | 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 Re: First Draft: cast(ref T)... as shorthand for *cast(T*)&... | ||||
---|---|---|---|---|
| ||||
Posted in reply to IchorDev | 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 Re: First Draft: cast(ref T)... as shorthand for *cast(T*)&... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Juraj | 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.
|