assert or throw in range members?

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Aug 5 10:31:25 PDT 2016


On 8/5/16 6:25 AM, Nordlöw wrote:
> Should range members front() and back() assert() or throw() on emptyness?
>
> If it should assert() doesn't that lead to unsafer code in release mode?
>
> What's the consensus here?

If the code is @safe, then it should trigger an error if you try to do 
unsafe things, regardless of asserts.

If you mark front or back or whatever @safe, and do unsafe things, then 
you will have to mark it as @trusted. In this case, instead of 
asserting, use if(cond) assert(0), or use enforce, as you can't allow 
unsafe behavior in a @trusted function based on the -release switch.

-Steve


More information about the Digitalmars-d-learn mailing list