Inferred Type for Explicit Cast
Jonathan Marler via Digitalmars-d
digitalmars-d at puremagic.com
Sat Dec 20 00:18:22 PST 2014
On Thursday, 18 December 2014 at 23:06:12 UTC, ketmar via
Digitalmars-d wrote:
> 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.
Performing a grep on phobos reveals there are currently almost
3,000 casts. I never like to use casts but they are a necessary
evil. I think anything D can do to help the programmer get their
job done is a win. The initial "pro" I saw for this idea was
improving refactoribility. I see this as a huge win. You claim
it will hide bugs? Could you give an example?
When I requested other people to chime in on this idea I was more
looking for data/examples. Maybe someone will think of an
example that shows this idea could encourage bad coding
practices? Maybe it will hide bugs? My point is, I don't want
to discourage you from providing your opinion, but I would
really like to understand where your opinion comes from. I hope
that wasn't harsh, I've read other posts you've made on the
forums and although I don't agree with everything you say I would
value your assessment. Thanks.
More information about the Digitalmars-d
mailing list