January 21, 2010
Sean Kelly wrote:
> Yeah, I was mostly responding to your earlier comment that casting didn't work for you. If you really wanted to cast it would be to ptrdiff_t anyway.
> 
> I agree that there's a more fundamental problem though. Weren't we going to make implicit narrowing conversions an error?  I know it isn't in the spirit of c, but c allows all sorts of horrible nonsense.

Implicit narrowing conversions are already disabled. But code involving cast is explicit.

Andrei

January 21, 2010
The code is reduced from a map that needs to hold both correspondences (ith to jth and jth to ith). But thanks for the point, it does apply to the simplified case.

Andrei

Steve Schveighoffer wrote:
> Fixed version:
> 
> import std.math, std.stdio;
> 
> void main() {
>     auto a = [ 4, 4, 2, 3, 2 ];
>     float avgdist = 0;
>     uint count;
> 
>     foreach (i, e1; a[0 .. $-1]) {
>         foreach (j, e2; a[i+1 .. $]) {
>             if (e1 != e2) continue;
>             count++;
>             avgdist += j+1;
>         }
>     }
> 
>     writeln(count, " ", avgdist / count);
> }
> 
> :)  I'm sure that's not what you were asking though...
> 
> -Steve
> 
> 
> 
> ----- Original Message ----
>> From: Andrei Alexandrescu <andrei at erdani.com>
>> To: Discuss the phobos library for D <phobos at puremagic.com>
>> Sent: Wed, January 20, 2010 11:50:20 PM
>> Subject: [phobos] [Fwd: "Unsigned-related bugs never occur in real code."]
>>
>> Is there anything we can do about this?
>>
>> Andrei
>>
>> -------- Original Message --------
>> Subject: "Unsigned-related bugs never occur in real code."
>> Date: Wed, 20 Jan 2010 20:42:50 -0800
>> From: Andrei Alexandrescu
>> Organization: Digital Mars
>> Newsgroups: digitalmars.D
>>
>> "It's an academic problem. Don't worry about it and move on."
>>
>> That's what Walter kept on telling me. Yet I've spent the better part of an hour reducing a bug down to the following:
>>
>> import std.math, std.stdio;
>>
>> void main() {
>>     auto a = [ 4, 4, 2, 3, 2 ];
>>     float avgdist = 0;
>>     uint count;
>>
>>     foreach (i, e1; a) {
>>         foreach (j, e2; a) {
>>             if (i == j) continue;
>>             if (e1 != e2) continue;
>>             ++count;
>>             avgdist += abs(i - j);
>>         }
>>     }
>>
>>     writeln(count, " ", avgdist / count);
>> }
>>
>> May this post be an innocent victim of the war against unsigned-related bugs.
>>
>>
>> Andrei
>> _______________________________________________
>> phobos mailing list
>> phobos at puremagic.com
>> http://lists.puremagic.com/mailman/listinfo/phobos
> 
> 
> 
> 
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos
January 21, 2010
On Jan 21, 2010, at 9:58 AM, Andrei Alexandrescu wrote:

> Sean Kelly wrote:
>> Yeah, I was mostly responding to your earlier comment that casting didn't work for you. If you really wanted to cast it would be to ptrdiff_t anyway.
>> I agree that there's a more fundamental problem though. Weren't we going to make implicit narrowing conversions an error?  I know it isn't in the spirit of c, but c allows all sorts of horrible nonsense.
> 
> Implicit narrowing conversions are already disabled. But code involving cast is explicit.

Sure, but if abs() took an int or long then it wouldn't compile without a cast, right?
January 21, 2010
Sean Kelly wrote:
> On Jan 21, 2010, at 9:58 AM, Andrei Alexandrescu wrote:
> 
>> Sean Kelly wrote:
>>> Yeah, I was mostly responding to your earlier comment that casting didn't work for you. If you really wanted to cast it would be to ptrdiff_t anyway.
>>> I agree that there's a more fundamental problem though. Weren't we going to make implicit narrowing conversions an error?  I know it isn't in the spirit of c, but c allows all sorts of horrible nonsense.
>> Implicit narrowing conversions are already disabled. But code involving cast is explicit.
> 
> Sure, but if abs() took an int or long then it wouldn't compile without a cast, right?

Unfortunately it does because int <-> uint and long <-> ulong automatically. We couldn't disable that.

What we can do is to use "negative overloading" - accept unsigned types but reject them via assert(false, "windows sucks");

Andrei

1 2
Next ›   Last »