Fetching an element using find
Steven Schveighoffer
schveiguy at gmail.com
Mon Jan 13 17:39:30 UTC 2020
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?
-Steve
More information about the Digitalmars-d-learn
mailing list