Is this a bug?

Is it possible to store different generic types? via Digitalmars-d digitalmars-d at puremagic.com
Sat Dec 10 00:16:48 PST 2016


On Saturday, 10 December 2016 at 08:09:00 UTC, Is it possible to 
store different generic types? wrote:
> I'm pretty sure the following code should work, but it doesn't.
>
> ```
> bool intersect(ptrdiff_t targetX, ptrdiff_t targetY, size_t 
> targetWidth, size_t targetHeight, ptrdiff_t x, ptrdiff_t y, 
> size_t width, size_t height)
> {
>  return targetX < x + width &&
>        x < targetX + targetWidth &&
>        targetY < y + height &&
>        y < targetY + targetHeight;
> }
>
> void main() {
> 	import std.stdio;
> 	writeln(intersect(0,0,800,600,     0,150,148,148));
> 	writeln(intersect(0,0,800,600,     -10,150,148,148));
> }
> ```
>
> It outputs:
> ```
> true
> false
> ```
>
> On the contrary if you write the same piece of code in other 
> languages ex. C#
>
> (Ran it through Linqpad)
> ```
> bool intersect(int targetX, int targetY, uint targetWidth, uint 
> targetHeight, int x, int y, uint width, uint height)
> {
>  return targetX < x + width &&
>        x < targetX + targetWidth &&
>        targetY < y + height &&
>        y < targetY + targetHeight;
> }
>
> void Main() {
> 	intersect(0,0,800,600,     0,150,148,148).Dump();
> 	intersect(0,0,800,600,     -10,150,148,148).Dump();
> }
> ```
>
> Then it outputs:
> ```
> true
> true
> ```
>
> Is it a bug or is it intended behavior?
Okay the issue seem to be that D casts the left-hand argument to 
the same type as the right-hand argument.

So when ex. doing "targetX < x + width" then it actually does 
"targetX < cast(width_type)x + width"

Where as I'd believe the behavior should have been "targetX < x + 
cast(x_type)width"



More information about the Digitalmars-d mailing list