opImplicitCast/opImplicitCastFrom

bearophile bearophileHUGS at lycos.com
Mon Oct 27 06:45:43 PDT 2008


Hxal:
> Implementing range checked numeric types would be my example of implicit casts' usefulness, but I'm sure a lot of people desire them.<

ObjectPascal programmers use them all the time, they seem to help avoiding some bugs in the program and to better state the meaning of certain variables.

In a modern language it's probably good for ALL integral numeric types to be range checked (when unspecified their range is the whole range their number of bits can represent).

D being a system language, has to allow such checks to be disabled into a module or into the whole program (so some syntax to single-module-disabling may be useful. ObjectPascals uses compiling setting to enable/disable it globally for the whole program, and {$R-} to disable rangle checking locally and {$R+} to enable it locally, locally overriding the compilation setting of the project).

I think that such checks are quite important to avoid bugs. But putting things into the language isn't enough: you have to put them into the head of D programmers and into the D culture. For example in Pascal is very common to use user-defined types to create a stricter (and safer?) type checking; D offers the typedef that allows to do the same (you just have to write 'typedef' for each line of code instead of using a typedef section like typedef: ...). But in my D programs for a long time I haven't used such typedef, and only in the last months I have started to use it more. Sometimes it's a matter of power or syntax (example: D contract programming isn't much powerful, so people may have less incentive to use it. Another example is that if a syntax is too much long, it becomes not convenient to be used, even if its semantic is good, for example certain operations done in functional languages, compared to the same operations done in D) but in the case of typedef I think it's mostly just a matter of being used to use it.

Bye,
bearophile



More information about the Digitalmars-d mailing list