@trusted methods

ag0aep6g anonymous at example.com
Fri Jun 18 12:57:02 UTC 2021


On 18.06.21 14:40, vit wrote:
> Are asserts enough to make method @trusted or is something like throw 
> exception or return error code necessary?
Asserts are a debugging feature. They're not suitable to ensure safety, 
because they're simply skipped in release mode.

`assert(false);` is the exception. It aborts the program even in release 
mode. So you can use it to bail out when your expectations fail.

So:

assert(expected); /* Don't do this. */

import std.exception: enforce;
enforce(expected); /* Instead, do this */
if (!expected) throw new Exception("..."); /* or this */
if (!expected) assert(false); /* or this. */


More information about the Digitalmars-d-learn mailing list