Motive behind !empty() with front() instead of Optional front()

IGotD- nise at nise.com
Wed Mar 24 22:33:56 UTC 2021


On Wednesday, 24 March 2021 at 21:20:40 UTC, Per Nordlöw wrote:
>
> Are you talking about a foreach loop? If so, the element type 
> will never have to be an optional type. Regardless of whether 
> `front` is wrapped in an Optional or not.

Could be a foreach loop but also a counted loop, you want to go 
through a certain amount of elements and you know for sure with 
check before that the elements exist. I'm not sure if I 
understand your statement, but wrapping such a loop with 
Optional<T> would lead to an extra overhead and since it is 
inside a loop it is not insignificant. I wouldn't trust an 
optimizer would understand how to remove such check.

Now the possibility is to have to versions of front, one that 
returns the element and another that returns Optional<T> (why do 
I write C++ syntax?). Not sure if that would lead to ambiguous 
case problems.


More information about the Digitalmars-d mailing list