Is opCast need, we have to!

Jesse Phillips jessekphillips+D at gmail.com
Fri Dec 3 04:26:53 PST 2010


foobar Wrote:

> I still haven't seen one example of generic code that uses a generic conversion. I remain unconvinced about this point and would appreciate an example.
> this is however a minor point in the discussion (stylistic issue) so let's move on to the more important parts.

Good point. I don't have one from experience. I can build on your example:

class Elderly : Person { 
   Elderly to(Person p) {
      Adult adult = to!Adult(p);
      return new Elderly(adult); 
   }
}

The above would throw an exception if p was a Kid. Note that your solution does not provide compile time checking because you'd still need a dynamic cast to retrieve an Adult from your person.

Providing a generic abstraction so when someone does find a use is a major style of Phobos.

> That was just one example. Let's discuss another one from your list then:
> uint foo = -1; // remember that this can be a result of a long calculation
> should the above compile in your opinion? 

No, I think int to uint should not be implicit. Maybe there is a reason other than C inheritance, I'm sure bearophile has a feature request for it.

> http://ideone.com/ksQDV 

I have an account so I get 15sec of runtime:

http://ideone.com/9ZvNL

Ok, I'm surprised this is successful. So I made another test that fails on my local machine, but strangely enough, succeeds on ideone...

http://ideone.com/gOtYc

Any way this demonstration is showing an  issue with overflow. I have already stated that explicit casts do not solve this and got no disagreement from you.

> I understand what you mean about the confirmation dialog but as you said yourself above, I'm not asking for a confirmation dialog but rather using safer actions. 
> I think we agree on this. 

Yes, we do agree. What we don't agree on is the best way to make things safer.

I am saying that implicit casts are not bad when data is not being lost.
And to! is a good candidate for providing safe conversions (possible data lost but qualifiers remain).
And that mixing conversions that loose data with ones that transform it (class conversion) is not an issue.


More information about the Digitalmars-d mailing list