My Kingdom For ...
Derek Parnell
derek at psych.ward
Fri Feb 22 15:15:23 PST 2008
On Fri, 22 Feb 2008 22:27:34 +0100, Don Clugston wrote:
> Janice Caron wrote:
>> On 22/02/2008, Derek Parnell <derek at nomail.afraid.org> wrote:
>>> Compile with the "-w" switch. It checks for 'missing' return paths.
>>
>> Ah great! Thanks. That will help a lot.
>>
>> I thought D didn't have warnings, but obviously I was wrong.
>>
>> It's a shame you can't /selectively/ enable warnings, because I don't
>> actually want to be warned about "length" shadowing. Still - it's
>> better than it's being an error. In general, I'd prefer that /all/
>> shadowing should be allowed. But that's another debate.
>>
>> Anyway, thanks for the info. Will definitely be using -w from now on.
>
> Really, 'missing return value' should be an error, not a warning. Unfortunately
> -w is horribly broken.
The "-w" does not mean "warning" in D. Walter does not believe in warnings.
In D, it means an optional error check, that is to say, one can optionally
choose to check for certain types of errors that are not checked for
without the "-w" switch. I think these are optional because there is some
debate over whether these things are actually errors or not.
The message text says 'warning' but the compiler treats them as errors
anyway.
> It generates some nasty spurious errors. For example, it
> won't let you perform a bitwise operation on anything other than an int.
>
> short a;
> short b = a | a; // won't compile with -w !!!!
Actually that turns out not to be the case. The 'warning/error' message in
this situatation informs the coder that there is a potential loss of
information when assigning a larger datatype to a smaller one.
"warning - test.d(4): Error: implicit conversion of expression
(cast(int)a | cast (int)a) of type int to short can cause
loss of data"
The real problem with this is that D decides to convert your 'short' values
to 'int' before doing the bitwise operation, leaving you with an 'int'
value to squeeze back into a 'short'. Now you and I know that this is a
redundant operation (converting to 'int') and that the high-end bits will
be zeroes anyway so that there is no loss of information. But DMD is not
that smart yet.
The 'warning' message is a valid one, in the general sense. For example
this code here gives a similar message ...
void main(string[] args)
{
short argc = args.length;
}
Even though we all know that we are not going to have 2^32 command line
arguments to the program.
The proper coding style to get the results you want is to tell the
compiler, and other readers of your code, that you "know what you are doing
so don't get upset"...
short a;
short b = cast(short)(a | a);
short argc = cast(short)args.length;
These do not give 'warning' messages now.
--
Derek Parnell
Melbourne, Australia
skype: derek.j.parnell
More information about the Digitalmars-d
mailing list