opImplicitCast/opImplicitCastFrom

bearophile bearophileHUGS at lycos.com
Mon Oct 27 09:07:10 PDT 2008


KennyTM~:

> so I thought you're talking about the subrange types in Pascal. I mean something like this:
>    var
>      x: 1..10;
>      y: 'A'..'Z';

Yes, something like that. Plus a syntax like this in D2:
short z;
becomes just a short way to write this:
ushort z: 0 .. 65535;
That is, all integral/char variables become checked range types :-)


> Do you mean automatic integer overflow checking (i.e. bounds checking)?

Yes (But note that here Delphi5 cheats a little).

-----------------

Simen Kjaeraas:

>So make all the normal built-in types (uint, int, float, etc) throw exceptions, and give access to lower-level types marked as unsafe. Basically, rename uint to 'uint_unsafe', and provide 'uint' as a typedef of Bounded!(uint_unsafe.min, uint_unsafe.max). I feel uint_unsafe is too long a name, but I'm sure something could be worked out (_uint?).<

I don't like that.
I think a better solution is to make all char/integral types become range checked by default when not in -release mode, plus add some syntax to disable the range check in a part of the code (or in a module), a bit like the "unsafe" statement of C#, and the {$R-} / {$R+} syntax of Delphi.

What syntax use for D to disable the integral range checks locally? Some possible syntaxes (that can be used for other kind of safeties too, like for the SafeD ideas):

unsafe (IntegerOverflow) { ...code... }
unsafe (IntegerOverflow): ...code...
unsafe (IntegerOverflow) expression;
safe (IntegerOverflow) { ...code... }
safe (IntegerOverflow): ...code...
safe (IntegerOverflow) expression;

Bye,
bearophile



More information about the Digitalmars-d mailing list