Inferred Type for Explicit Cast

ketmar via Digitalmars-d digitalmars-d at puremagic.com
Thu Dec 18 15:30:23 PST 2014


On Thu, 18 Dec 2014 23:18:16 +0000
"Adam D. Ruppe via Digitalmars-d" <digitalmars-d at puremagic.com> wrote:

> On Thursday, 18 December 2014 at 23:06:12 UTC, ketmar via 
> Digitalmars-d wrote:
> > the only thing this will help is to hide bugs, i believe.
> 
> On the contrary, I find explicit casts hide bugs. Suppose you 
> write:
> 
> size_t a = cast(int) b;
> 
> It will compile and run. It'll mostly work. But the cast to int 
> probably wasn't intended (it was probably written in 32 bit code 
> and not correctly ported to 64 bit).
> 
> How often do we also write auto a = cast(T) b;? The difference 
> would be the type is written on the left side instead of the 
> right. Might make an important differnce when calling functions.
> 
> I think the auto cast is a win all around.

explicit cast are immediately decipherable (except of
`cast(typeof(result))` maybe). i.e. `cast(int)` is surely casts to
`int`. but what is `cast(auto)`? to what type it will cast the value?
this makes the language unnecessary puzzling, and will not save from
bugs anyway.

`size_t a = cast(int)b;`? so `b` must be long/ulong, and the author
guarantees that b will never be bigger than `int.max`. perfect. in no
way `cast(auto)` will save us from the bug here: it will simply hide
the author intentions.
-------------- 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/20141219/5c07f1e1/attachment-0001.sig>


More information about the Digitalmars-d mailing list