Cast to left hand side

eles via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Nov 9 13:47:02 PST 2014


On Sunday, 9 November 2014 at 19:00:01 UTC, tcak wrote:
> In some cases, I need to cast right hand side expression to 
> left hand side. While it looks/feels simple for basic data 
> types, it requires long lines with duplication when flexible 
> code is desired to be written.
>
> Example:
>
> int a = 7;
> byte b;
>
> b = cast( byte )a;

I am also strongly in favor of introducing an "uncast". For 
example, in C++'x const_cast and in D's cast for removing, for 
example immutability:

immutable int* p = ...;
int* q = cast(int*)p;

I think the goal is not clearly expressed with this cast. It does 
not show that it's intension is to remove immutability and 
otherwise let that type unchanged. If later a mismatch is 
introduced between the left and the right type of data, that 
inoffensive cast could create problems by hiding an error that 
should have been spotted.

Something like that would be more expressive:

immutable int* p = ...;
int* q = uncast(immutable)p;
//or
int* q = cast(~immutable)p;

This way, invalid implicit conversions from p's type to q's type 
would be spotted.


More information about the Digitalmars-d-learn mailing list