automatic int to short conversion - the HELL?
Tomas Lindquist Olsen
tomas at famolsen.dk
Thu Sep 18 12:29:41 PDT 2008
Ary Borenszweig wrote:
> Chris R. Miller wrote:
>> downs wrote:
>>> Chris R. Miller wrote:
>>>> downs wrote:
>>>>> Jarrett Billingsley wrote:
>>>>>> On Wed, Sep 17, 2008 at 10:26 PM, downs <default_357-line at yahoo.de>
>>>>>> wrote:
>>>>>>> void main() { int i; short x; x = i; }
>>>>>>>
>>>>>>> Excuse me, but - how exactly is it that this is in any way, shape or
>>>>>>> form valid code?
>>>>>>>
>>>>>>> How can I trust a language that allows those kind of shenanigans?
>>>>>>>
>>>>>> lern2warningsflag.
>>>>> "Warning. Your code is broken."
>>>>>
>>>>> I still claim it should actually be an error, although the only
>>>>> practical and correct solution might be full ranged type support.
>>>> I don't get it. Why can that not be simple implicit type casting?
>>>
>>> Because short is not a superset of int.
>>
>> Well.... then it's just a loss of precision warning like on every
>> other language (Java and C++ off the top of my head).
>>
>> -w and be on thy way, unless I'm missing something else.
>
> 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:
>
> long x = ...;
> int y = (int) x; // yes, I know I might loose information, but I'm sure
> // it won't happen
>
> However, if you see this code (in D):
>
> long x = ...;
> int y = x;
>
> you start wondering whether the original author simply forgot to add the
> cast or he knew what he was doing. How can you know?
>
> I like the compiler to force you to write an explicit cast. It is
> saying: "Hey, please tell me you know what you are doing here... because
> maybe you didn't notice you might loose information here".
Doesn't all this come down to convincing Walter to lose the "must follow C rules" mantra? I
doubt that's gonna happen ...
Who knows :)
- Tomas
More information about the Digitalmars-d-learn
mailing list