Consider generalizing Bounded

Tomek Sowiński just at ask.me
Tue Oct 12 16:39:03 PDT 2010


It may sound ridiculous but this is what came to me in a dream last night: why exactly does 
Bounded have to express an interval? Forget intervals for a minute:

struct Bounded(alias Pred, T);

where Pred is a unary callable returning bool (same as in e.g. filter()).

This opens a host of possibilities:
1. sin(x) - make sure x is in e.g. (-PI/2 + 2*k*PI, +PI/2 + 2*k*PI)
2. constrain a matrix to be non-singular
3. a date variable only assignable to working days
4. don't let a missle fly off the level's bounding box in an FPP shooter
5. work exclusively with non-null references
6. ...

With all the power coming from the abstraction, it may still be worth exposing Bounded(min, 
max, T), a wrapper over the general form to honor a common usecase. I expect candidates 
for other wrappers to arise as bounds are laid.

Also, 'Confined' is a name that advertises the more general functionality better and is free of 
the 2-knots-on-a-ribbon mental payload.

What you think: dream or nightmare?

-- 
Tomek


More information about the Digitalmars-d mailing list