Is opCast need, we have to!

bearophile bearophileHUGS at lycos.com
Wed Dec 1 14:32:12 PST 2010


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


More information about the Digitalmars-d mailing list