between and among: worth Phobosization?
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Mon Dec 16 14:30:28 PST 2013
On 12/16/13 2:24 PM, Jakob Ovrum wrote:
> 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.
I guess if we need several versions of between, we could give up on it.
> 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.
A custom predicate defaulted to '==' would be the ticket.
Andrei
More information about the Digitalmars-d
mailing list