Is there a nicer way to get the first element or typeof(element).init from a range?

Paul Backus snarwin at gmail.com
Mon May 31 11:40:11 UTC 2021


On Sunday, 30 May 2021 at 12:16:19 UTC, realhet wrote:
> Is there a prettier way to do this?
>
> Thanks in advance.

```d
import std.range;

auto getFirst(R)(R range)
     if (isInputRange!R)
{
     if (range.empty) return ElementType!Range.init;
     else return range.front;
}
```


More information about the Digitalmars-d-learn mailing list