
Deane Yang wrote:
Edward Diener wrote:
Mathias Gaunard wrote:
Apart from that, wouldn't it be more interestin, when multiplying a bounded_int
by 2, to yield a bounded_int instead of attempting to keep the object with the same constraint? Of course, that only works with bounded_int and not constraints in general. I disagree with this also. The entire idea of a constraint in this library is that the underlying value meets the criteria of the constraint and not that the constraint changes to accomodate a new underlying value.
I have an even more extreme view on this. I don't believe any arithmetic operations should be permitted for constrained numbers. They just don't make sense (would you ever want to add two different days of a month?).
You have chosen a particular type for which arithmetic operations sometimes make little sense. In many other situations this is not the case.
There are occasions where you do want to perform an algorithm that does make sense but where arithmetic operations are performed along the way. But in those cases I want to be forced to do an explicit cast to an unconstrained type.
It becomes inconvenient to use an explicit cast. It is more flexible that a constraint can transform to its underlying type whenever necessary, but still maintain the constraint after the operation occurs.
I would add that I think the concept of constrained values is tightly linked to the concept of units (by which I mean the concept of wrapping a number type by a meaningful type), as well as the concept of an algebraic type (where you constrain the algebraic operations that can be performed). I would love to see someone put this all together into a single coherent library.
There is already a units library in 1.37. Perhaps you want constraints added to this library.