between and among: worth Phobosization?

Jakob Ovrum jakobovrum at gmail.com
Mon Dec 16 14:24:55 PST 2013


On Monday, 16 December 2013 at 22:10:28 UTC, Walter Bright wrote:
> Exactly, meaning I'd have to go look at the source code for it, 
> whereas with the latter I can see right away what it is. The 
> function is a net programmer time loss.

Surely you would turn to the documentation, not the source code. 
We could make it require the bounds template parameter, so it 
would always be required to use it like:

if(val.between!"[)"(0, 10))
    ...

However, with a good, solid default that is easy to remember, I 
wouldn't mind having a default argument for the bounds.

At any rate, the implementation of `among` should probably be 
something closer to:

---
uint among(T, Values...)(auto ref T value, auto ref Values values)
     if(!is(CommonType!(T, Values) == void))
{
     foreach(uint i, ref v; values)
     {
         if(value == v) return i + 1;
     }

     return 0;
}
---

It would of course be even better if we had a nice and clear way 
to express that all types in Values must be equatable (?) with T, 
something like `allSatisfy!(isComparable!("==", T), Values)`, 
where `isComparable` is the missing link.


More information about the Digitalmars-d mailing list