Cast to left hand side
ketmar via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Nov 9 14:14:30 PST 2014
On Sun, 09 Nov 2014 21:47:02 +0000
eles via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com> wrote:
> 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.
i believe you can do this with some template magic.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20141110/a72af6ad/attachment.sig>
More information about the Digitalmars-d-learn
mailing list