Are contracts intended for verifying @safety;
ag0aep6g via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Nov 7 11:30:01 PST 2016
On 11/07/2016 06:30 PM, Somebody wrote:
> void moveFrom(int[] a, in int[] b) @trusted in{
> assert(a.length >= b.length);
> } body {
> memmove(cast(void*)a.ptr, cast(void*)b.ptr, b.length * int.sizeof);
> }
>
> Is this ok? And if not, how should it be done, preferably without
> changing the condition or memmove call?
No, it's not ok. Contracts and (most) asserts are not included in
release builds, but the rules for @safe/@trusted don't change. So you'd
be breaking @safe in release builds.
You can use std.exception.enforce instead, in the body instead of a
contract:
void moveFrom(int[] a, in int[] b) @trusted
{
import std.exception: enforce;
enforce(a.length >= b.length);
memmove(/* ... as above ... */);
}
More information about the Digitalmars-d-learn
mailing list