extend "in" to all array types

Brad Anderson eco at gnuk.net
Wed Jan 15 09:47:18 PST 2014


On Wednesday, 15 January 2014 at 17:29:07 UTC, Tobias Pankrath 
wrote:
> On Wednesday, 15 January 2014 at 17:13:37 UTC, Meta wrote:
>>
>> auto inWrap(T)(T[] arr)
>> {
>>    static struct InWrapper
>>    {
>>        import std.typecons: Nullable;
>>
>>        private T[] payload;
>>
>>        Nullable!size_t opBinaryRight(string op: "in")(T val)
>>        {
>>            Nullable!size_t index;
>>            foreach (i, element; payload)
>>            {
>>                if (element == val)
>>                {
>>                    index = i;
>> 					
>> 					return index;
>>                }
>>            }
>>
>>            return index;
>>        }
>>    }
>>
>>    return InWrapper(arr);
>> }
>>
>> void main()
>> {
>>    auto arr = [0, 1, 2, 3, 4];
>> 	auto i = 2 in arr.inWrap;
>>    assert(!i.isNull);
>> 	assert(i == 2);
>> }
>
> The in-operator should return T* for consistency with the 
> build-in ones.

I was thinking recently it'd be even more convenient if it 
returned std.typecons.Nullable. Then you get alias this and 
aren't having to constantly dereference it yourself.


More information about the Digitalmars-d mailing list