Range analysis result printing?

2013-06-23 Thread bearophile
I am thinking about opening an enhancement request, but this time 
I first prefer to ask your opinion here.


For this code:

void main() {
ubyte x;
ubyte y = x  1;
}


The range analysis determines that it's conceivable to the result 
of that expression to not fit in y, so the D compiler 2.064alpha 
gives:



temp.d(3): Error: cannot implicitly convert expression 
(cast(int)x  1) of type int to ubyte



To help the programmer understand faster the mistake in his/her 
code when expressions become more complex I think it's also 
useful to print the range resulting from the analysis, like:



temp.d(3): Error: cannot implicitly convert expression 
(cast(int)x  1) in interval [0 ... 510] of type int to ubyte



It uses 3 dots because it's an interval that includes the right 
end. Otherwise if you print an interval open on the right in a 
case like this you have to print a ulong.max+1 value:



void main() {
ulong x;
int y = x;
}


Do you like?

Bye,
bearophile


Re: Range analysis result printing?

2013-06-23 Thread Jonathan M Davis
On Sunday, June 23, 2013 16:20:51 bearophile wrote:
 It uses 3 dots because it's an interval that includes the right
 end.

The way that you normally indicate exclusive and inclusive intervals in math 
is ) vs ], where ) is exclusive and ] is inclusive. Some folks will understand 
that. I don't think that anyone will understand that ... says anything about 
whether the end is inclusive or exclusive - not unless that's commonly used 
somewhere else that I'm not familiar with.

- Jonathan M Davis


Re: Range analysis result printing?

2013-06-23 Thread bearophile

Jonathan M Davis:

The way that you normally indicate exclusive and inclusive 
intervals in math
is ) vs ], where ) is exclusive and ] is inclusive. Some folks 
will understand
that. I don't think that anyone will understand that ... says 
anything about
whether the end is inclusive or exclusive - not unless that's 
commonly used somewhere else that I'm not familiar with.


I agree that such mathematical syntaxes are more commonly known 
than the ... syntax that is used in Perl and I think Ruby and few 
other languages.


In other nations mathematicians use [x, y[ to represent open or 
close intervals (that syntax is used in std.random too).


So maybe instead of (in interval x ... y) it's better to use (in 
interval [x, y]) and hope people will understand this doesn't 
follow the normal D/Python usage of closed-on-the-right intervals.


I have added a note in the ER:
http://d.puremagic.com/issues/show_bug.cgi?id=10455

Bye,
bearophile