dmd 1.046 and 2.031 releases
Chad J
chadjoan at __spam.is.bad__gmail.com
Mon Jul 6 12:04:19 PDT 2009
Walter Bright wrote:
> Denis Koroskin wrote:
>> I'd put an assert and mad a case explicit, if there is a size_t is so
>> badly needed for ptr difference:
>>
>> assert(p1 >= p2);
>> size_t y = cast(size_t)p1 - p2;
>
> Aside from the typo in that code (!) the problem with casts is they are
> a sledgehammer approach. Casts should be minimized because they *hide*
> typing problems in the code. The more casts in the code, the more the
> type-checking abilities of the compiler are disabled. I suspect this
> will *hide* more bugs than it reveals.
>
> The reality is is that most integers used in programs are positive and
> relatively small. int and uint are equally correct for these, and people
> tend to use both in a mish-mash. Trying to build a barrier between them
> that requires explicit casting to overcome is going to require a lot of
> casts that accomplish nothing other than satisfying a nagging, annoying
> compiler.
>
> I've used such a compiler - Pascal back in the early 80s. All the casts
> it required me to insert basically sucked (and never revealed a single
> bug). When I discovered C with its sensible system of implicit casting,
> it was like putting on dry clothes after being soaked out in the cold rain.
In the context of a sign-sensitive language
assert(p1 >= p2);
size_t y = cast(size_t)p1 - p2;
looks to me like it is equivalent to
size_t y = p1 - p2;
but in the context of a sign-insensitive language.
The difference in behavior is that the former has a runtime assert,
which is arguably useful. The difference in aesthetics/maintainability
is that the former has that undesirable cast in there.
Perhaps we can have the best of both worlds, and just make the latter
work but automatically insert the given runtime assert while in
debug/non-release mode.
So in D2 the code
size_t y = p1 - p2;
would become
assert(p1 >= p2);
size_t y = p1 - p2;
during compilation.
Sure having negative indices around would eventually crash the program
anyways, but it's really helpful to have the program crash as close to
the bug's location as possible.
(Also not seeing that typo. dmd seems to think it's alright
syntactically, and don't we want p1 to be greater?)
More information about the Digitalmars-d-announce
mailing list