December 29, 2014 Re: Inferred Type for Explicit Cast | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | On Monday, 29 December 2014 at 16:45:31 UTC, Dicebot wrote:
> On Monday, 29 December 2014 at 15:50:15 UTC, Andrei Alexandrescu wrote:
>> I see. I guess it's easy to add std.conv.explicitCast and std.conv.implicitCast if there's enough impetus for it. -- Andrei
>
> Yes, exactly. That was why I have asked general opinion about it - don't want to add yet another utility no one uses. I will probably go and just add it anyway : https://github.com/D-Programming-Language/phobos/pull/2822
+1
|
December 29, 2014 Re: Inferred Type for Explicit Cast | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan Marler | On 12/29/14 10:14 AM, Jonathan Marler wrote:
> On Monday, 29 December 2014 at 16:45:31 UTC, Dicebot wrote:
>> On Monday, 29 December 2014 at 15:50:15 UTC, Andrei Alexandrescu wrote:
>>> I see. I guess it's easy to add std.conv.explicitCast and
>>> std.conv.implicitCast if there's enough impetus for it. -- Andrei
>>
>> Yes, exactly. That was why I have asked general opinion about it -
>> don't want to add yet another utility no one uses. I will probably go
>> and just add it anyway :
>> https://github.com/D-Programming-Language/phobos/pull/2822
>
> +1
If you like it, please review it. It took me all of 30 seconds to find a typo. Thanks. -- Andrei
|
December 29, 2014 Re: Inferred Type for Explicit Cast | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan Marler | On Sunday, 21 December 2014 at 08:23:58 UTC, Jonathan Marler wrote: > On Sunday, 21 December 2014 at 03:04:05 UTC, Steven Schveighoffer wrote: >> On 12/20/14 3:36 AM, Jonathan Marler wrote: >> > int y; > ushort x = cast(byte, int)y; // cast from int to byte > > This should definitely produce an error. This code could exist if x was initially a byte and was later changed to a ushort. I think this feature would make casts alot safer because any time a type was changed you would get an error saying you need to go update all your casts. What do you think? This kinda helps for the other idea that I was mentioning: dissociate the data format/size of the access type when casting. I still find very ugly, in C++, to specify a type to cast to for const_cast. It exposes the same problem, when one type change, you remain with an invalid cast. In C is the same, and even worse. You have to specify that the cast is performed to remove "const"-ness, for example, in *the comments*. If the type change, you have an invalid cast, but the compiler will swallow it happily. Should have "access_cast". That will only throw away const and let the type format/size unchanged. Implicit casting (or another explicit cast) will have to play the game to ensure type compatibility, not the remaining (kinda of) reinterpret_cast. |
Copyright © 1999-2021 by the D Language Foundation