From Ada 2012
H. S. Teoh
hsteoh at quickfur.ath.cx
Thu May 3 17:11:29 PDT 2012
On Fri, May 04, 2012 at 01:49:32AM +0200, bearophile wrote:
[...]
> I have not used Ada a lot, but I like how you usually define (strong)
> types for most classes of variables, like for integral values, each
> with their range, if they are sub-ranges (subtypes) of other strong
> ranges, and so on.
[...]
This makes me wonder if such a thing is implementable in D without
sacrificing efficiency. Perhaps have a series of templated structs that
wrap around an int, that keep track of the range? TDPL gives an example
of an int wrapper that checks for overflow. I think it might be
possible to write an int wrapper, say RangedInt!(min,max) that enforces
a certain value range. Member functions can handle cross-range
computations (e.g., RangedInt!(1,10) is assignable to RangedInt!(0,15)
without any checks, but the converse assignment will have a check,
either runtime or compile-time if possible, to ensure the assigned value
is within range).
You can also version the thing to omit range checks in -release mode or
have the user specify some version=... to omit range checks, if he
wishes to have maximum efficiency after extensive testing to ensure
range violations don't happen.
T
--
Frank disagreement binds closer than feigned agreement.
More information about the Digitalmars-d
mailing list