Inferred Type for Explicit Cast

ketmar via Digitalmars-d digitalmars-d at puremagic.com
Sat Dec 20 11:57:42 PST 2014


On Sat, 20 Dec 2014 19:26:54 +0000
Jonathan Marler via Digitalmars-d <digitalmars-d at puremagic.com> wrote:

> Hmmmm...an interesting concept.  Make unsafe things harder to do 
> to discourage their use.  I'm not sure if I agree but maybe you 
> can convince me. Where do you draw the line?
the thing is that they aren't only harder to use, but they are also
explicitly marked as "system" things. i.e. things that should be used
only if you are *really* know what you're doing.

besides, code with `system.cast()` looks suspiciously "burden", which
also signals that this is not a thing one should mindlessly use
everywhere.

something like that.

> is it requires less maintenance. Say you have the following:
> 
> void myfunc(uint x)
> {
>      ubyte val;
>      //
>      // ...
>      //
>      val = cast(ubyte)x;
> }
> 
> Now let's say that you need change the value of 'val' to ushort.
> 
> void myfunc(uint x)
> {
>      ushort val;
>      //
>      // ...
>      //
>      val = cast(ubyte)x;
> }
why do you need `cast` here? isn't `to!ubyte` looks better? and it's
not only looks better, it will provide you additional overflow checks
too. so if you'll throw a '1024' in your unittest, the second version
will fail, signalling that something is going wrong.

> Now I'm not saying that cast(auto) is good in all cases, I'm just 
> trying to get you to see the big picture here.  cast(auto) could 
> be useful and could help prevent maintenance bugs in SOME cases.
i see that `cast` should NOT be used in that cases at all. and this
returns us to "`cast` is hack. avoid the hacks!"

> Yes it can be misused, but I don't agree that making it harder to 
> use is a good way to prevent misusage, but requiring more 
> verbosity is good.  You may disagree that "cast" is enough 
> verbosity but I think that's a matter of opinion.
sure, i'm just talking about what i see as "good", i'm in no way trying
to tell that my opinion is the best one. sorry if my words are too
"forcing". i'm a somewhat sarcastic person IRL, and i don't use English
in my everyday life, so i can sound too hard sometimes, failing to
properly translate my twisted style of writing. ;-)
-------------- 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/20141220/6b5e5b80/attachment.sig>


More information about the Digitalmars-d mailing list