automatic int to short conversion - the HELL?

bearophile bearophileHUGS at lycos.com
Fri Sep 19 03:40:14 PDT 2008


Ary Borenszweig:
> No, no. In Java it's an error, an explicit cast is required.
> http://www.programmersheaven.com/2/FAQ-JAVA-Type-Conversion-Casting
> Which is perfect. It expresses the intents of the programmer:

About such matters I suggest you all to also take a look at how Ada works. Ada was designed first of all to create reliable software, so avoiding casting-derived bugs too is essential.
D tries to avoid some of the pitfalls of C, to be a language less bug-prone: casts is where D has to improve still in such regards. Time ago (when I was more a D newbie) I have already had a bug in my code because of a casting bug:

import std.stdio;
void main() {
    int n = -5;
    int[] a = [1, 2, 3];
    writefln(a.length > n); // prints false
}

A well designed language, even a system language like Ada or D, must avoid such kinds of bugs, regardless the amount of ignorance of the programmer.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list