extend "in" to all array types
Tobias Pankrath
tobias at pankrath.net
Wed Jan 15 09:29:05 PST 2014
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.
More information about the Digitalmars-d
mailing list