Rant: Date and Time fall short of simplicity in D
Jonathan M Davis
jmdavisProg at gmx.com
Sat Mar 30 14:27:37 PDT 2013
On Saturday, March 30, 2013 09:53:49 Artur Skawina wrote:
> You can't tell if there is an opCast w/o looking at S. So it's either
> a perfectly fine conversion, no-op, a potentially dangerous operation, or
> an error. The compiler would have been able to catch the error, but by
> overloading 'cast' you've prevented it from doing that.
It would only be an error because the compiler couldn't do the cast (and I
believe that unless the memory layout is the same, casting between two structs
without opCast doesn't work, and classes will only give an error if neither
class is a base class of the other), so defining opCast eliminates any need for
any error.
But regardless, if you use std.conv.to rather than casting directly, then you
don't have to worry about whether opCast is defined or not. If it is or
std.conv.to can convert the type in another way, then std.conv.to will work,
and if there is no opCast and none of std.conv.to's conversions will work,
then you'll get an error. It won't try to explicitly cast unless the type has
defined opCast.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list