Inferred Type for Explicit Cast

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


On Thu, 18 Dec 2014 22:46:04 +0000
Jonathan Marler via Digitalmars-d <digitalmars-d at puremagic.com> wrote:

> What are peoples thoughts on having an inferred type for "cast"? 
> Good/Bad idea? If good, how helpful would this be? Would this 
> break existing code somehow? I think this feature would be a nice 
> added convenience.  Not super helpful but nice to have.
> 
> Here's the details
> -------------------
> Typed Cast: cast(T)v
>    try to cast v to T
> Type Inferred Cast: cast(auto)v
>    try to cast v to whatever type is required in the current 
> context
> 
> void foo(string s)
> {
>    // ...
> }
> void main()
> {
>      const(char)[] s = "hello";
>      foo(cast(string)s); // Current
>      foo(cast(auto) s);  // The type of the cast is inferred to be 
> a string
>      foo(cast() s);      // Another possible syntax
> }
> 
> This would help refactoribility.  If a function argument changes 
> it's type, and the caller is using a cast, then the caller's cast 
> type will be updated automatically.  Note that if it changes to 
> an invalid type, the cast will still fail like normal.

using casts is a bad practice. and "auto casts" is... i can't even find
a word. the only thing this will help is to hide bugs, i believe.

please, don't.

nothing personal, i'm just terrified by the idea.
-------------- 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/c71fadd6/attachment.sig>


More information about the Digitalmars-d mailing list