Inferred Type for Explicit Cast

ketmar via Digitalmars-d digitalmars-d at puremagic.com
Sun Dec 21 08:08:50 PST 2014


On Sun, 21 Dec 2014 15:20:44 +0000
Jonathan Marler via Digitalmars-d <digitalmars-d at puremagic.com> wrote:

> It seems alot of the potential bugs from casting can occur when 
> one of the types change.  If we forced the cast operator to match 
> the type(s) exactly then it would eliminate all these bugs.
that is what i mean by "don't use hacks"! what many people think about
when there is need to convert from one type to another? CAST IT! not
"convert it", but "cast it". but we have idiomatic "to!" for type
conversion.

still puzzled a little? ;-) i'm about "hide that cast in system module
in faraway land" again.

  int z;
  short a;
  z.castassignto!(short)(a); // OK
  z.castassignto!(byte)(a); // FAIL

this is ugly? yes, exactly as it should be. this is working? yes. we
can use it to enforce both types too:

  castassignto!(int, short)(z, a);

will this work? oops, nope... as we have no control on argument type
deduction for templates, one can write:

  z.castassignto!(a);

ahem... is this `cast(auto)`? ;-)


ok, to make a long story short: the idea is to replace explicit casts
by templated casts. templates gives us more control, can have more
arguments and if we'll do a ER that allows us to control argument type
deduction will give us way to control what must be specified and what
can be omited.
-------------- 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/attachments/20141221/5baf8c26/attachment.sig>


More information about the Digitalmars-d mailing list