integer division with float result

0ffh frank at frankhirsch.youknow.what.todo.net
Sun Nov 18 20:29:38 PST 2007


Bruce Adams wrote:
> 0ffh Wrote:
>> With return type overloading I get
>> 
>> int i,j; float f; [...] i=i/j; // implicit int f=i/j; // implicit 
>> float foo(i/j); // compiler error: under-determined type information 
>> foo(cast(int)i/j); // explicit  int
 >> foo(cast(float)i/j); // explicit float
 >> [...btw. thunderbird rewrapping does strange things to those lines...]
> 
> Well I wasn't advocating a change to D (yet). Hence the non D in my 
> original post. It was more a thought experiment. Your use case above is
> certainly a compelling reason not to do it this way, at least for 
> division. I wonder if there is not a way of having the best of both 
> worlds. Still, I begin to see why others want a seperate div operator.
 > I shall ponder some more...

Far be it from me to add to the plethora of D feature requests, but
one possible solution might be giving an explicit precedence hierarchy
to the compiler at one point, e.g.

void foo(int){...}
void foo(float){...}
precedence foo(float) > foo(int);

could in case of ambiguity choose float over int.
But I'm not quite sure this could not backfire in some way...

Regards, frank



More information about the Digitalmars-d mailing list