Is opCast need, we have to!
Sclytrack
sclyrack at idiot.com
Wed Dec 1 22:36:23 PST 2010
== Quote from bearophile (bearophileHUGS at lycos.com)'s article
> foobar:
> > 1. static_cast:
> > a. Remove ALL implicit coercions inherited from c such as double -> int,
> > b. I don't see the need for an operator for conversions since they can have
different parameters, e.g.:
> > - converting to string can have formatting specified
> > - converting string to numeric types with optional base parameter
> > - converting integer to floating point specifies round/floor/ceiling/etc..
> > 2. const_cast: should be a _separate_ operator in order to prevent removing
const by mistake.
> > const Base obj1 = new Derived();
> > auto obj2 = cast(Derived)(obj1); // oops: meant to only down cast
> > 3. dynamic_cast: the language should provide a down cast operator for OO.
> > 4. reinterpret_cast: unsafe and should be restricted as much as possible (Not
available in @safe code) maybe not provide it at all since it's implementable via
union. A restricted library solution for when you really want to play with bits
and bytes?
> There are some ideas here, like the separation from const_cast, dynamic cast,
and other casts. In past I too have asked for something similar, but I think
Walter was not interested.
> I guess the idea of having a single cast is to keep the language simple. But
those _are_ different kinds of casts, they have different semantics. Lumping
different semantics into the same syntax is not a good way to simplify a language,
in my opinion. It just creates obscurity and maybe some other troubles too.
> Bye,
> bearophile
How about the following.
T1 a;
T2 b;
b = cast(T2, T1) a;
The difference between the two types would be clearly visible.
More information about the Digitalmars-d
mailing list