Is this reasonable?
Jonathan M Davis
jmdavisProg at gmx.com
Fri Dec 6 10:12:25 PST 2013
On Friday, December 06, 2013 18:28:09 monarch_dodra wrote:
> On Thursday, 5 December 2013 at 18:26:48 UTC, Jonathan M Davis
>
> wrote:
> > To be fair, you can't solve the problem automatically. It's
> > fundamentally
> > wrong to compare signed and unsigned values, and doing either
> > the conversion
> > to unsigned or to signed could be wrong (or both could be
> > wrong), depending on
> > the values.
>
> *Actually*... that's not exactly true.
>
> std.algorithm.max and std.algorithm.min actually do it perfectly
> well.
>
> It is completely feaseable to imagine having an:
> std.functional.less(T, U)(T t, U u){}
>
> Which could work for *any* (compatible) type, including those
> with mixed signed-ness.
>
> The only "issue" is that if the signs *are* mixed, the comparison
> is a *bit* more costly. It involved *two* comparisons, and a
> boolean condition:
> https://github.com/D-Programming-Language/phobos/blob/ab34fb92addca61755474d
> f04a0d0d6e0f1148a2/std/algorithm.d#L6698
>
> Truth be told, "a < b" could work correctly 100% of the time, no
> question asked, if the *language* wanted to. The (2) problems
> would be:
> 1) Breaks backward compatibility with C and C++.
> 2) Higher (and hidden) comparison costs.
>
> Try to convince Walter that these are worth it XD !
Ah, good point. I was assuming that you would just doing a straight up
comparison, in which case, you have to pick either signed or unsigned, whereas
if you can convert both ways and do both comparisons, that does change things
a bit. But still, I wouldn't expect Walter to ever agree to something like
that, and honestly, I think that making it an error and requiring the
programmer to deal with it is still a better option anyway. They can then use
a function to do the comparison that you're describing when they actually want
that and thus only incur the cost when that's what they really want. The key
is just to make sure that you don't silently do the single conversion and
comparison and end up with a bug. How to best do the comparison depends on
what you're trying to do.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list