Do you like bounded integrals?

Bill Hicks via Digitalmars-d digitalmars-d at puremagic.com
Wed Aug 24 19:27:45 PDT 2016


On Tuesday, 23 August 2016 at 20:40:06 UTC, Andrei Alexandrescu 
wrote:
> Currently checkedint 
> (https://github.com/dlang/phobos/pull/4613) stands at 2432 
> lines and implements a variety of checking behaviors. At this 
> point I just figured I can very easily add custom bounds, e.g. 
> an int limited to 0 through 100 etc. It would take just a few 
> lines because a lot of support is there (bounds hooks, custom 
> min/max) anyway.
>
> However, I fear it might complicate definition and just be a 
> bit much. Here's the design I'm thinking of. Current:
>
> struct Checkedint(T, Hook = Abort);
>
> Under consideration:
>
> struct Checkedint(T, Hook = Abort, T min = T.min, T max = 
> T.max);
>
> It's easy to take the limits into account, but then there are a 
> few messes to mind:
>
> * When assigning a Checked to another, should the limits be 
> matched statically or checked dynamically?
>
> * When composing, do the limits compose meaningfully?
>
> * How to negotiate when both the user of Checked and the Hook 
> need to customize the limits? (e.g. if you look at WithNaN it 
> needs to reserve a special value, thus limiting the 
> representable range).
>
> I think all of these questions have answers, but I wanted to 
> gauge the interest in bounded checked integrals. Would the need 
> for them justify additional complications in the definition?
>
>
> Andrei

It's just an abortion.

There is a C++ bounded::integer library by David Stone that looks 
much better than what you have here.  For a language that's 
claiming to be a better C++ and with better CT features, I would 
expect something more elegant and useful.  Besides, this only 
works with integers.  You've been bashing Go for lack of 
generics, so how about we see something that works with other 
types too, no?

Perl 6:

subset MyInt of Int where (4 < * < 123);

or for string,

subset MyStr of Str where (0 < *.chars < 100);

Just beautiful, and what you would expect from a language that's 
trying to be better than the past.




More information about the Digitalmars-d mailing list