On Mon, Jan 13, 2020 at 12:39:30PM -0500, Steven Schveighoffer via Digitalmars-d-learn wrote: > I have a range (array), I want to find an element and get it from that > array. > > The code I'm writing looks like this: > > auto answer = arr.find!((item,x) => item.id == x)(id); > enforce(!answer.empty, "id not in the range"); > auto realAnswer = answer.front; > > I can't do find(id).front, because that relies on asserts, which throw > errors and kill the program, and are not always compiled in. I need an > exception thrown so I can recover and report the error to the user. > > But I hate to write 3 lines of code to do this every time. > > I'm not seeing an easy solution in Phobos, am I missing it? [...]
Why not write your own convenience wrapper? auto firstElement(R)(R r) if (isInputRange!R) { if (r.empty) throw new Exception(...); return r.front; } auto e = myData.find!(e => blah(e)).firstElement; T -- I think Debian's doing something wrong, `apt-get install pesticide', doesn't seem to remove the bugs on my system! -- Mike Dresser