Good Contract programming idiom?
Lars T. Kyllingstad
public at kyllingen.NOSPAMnet
Tue Mar 2 09:36:22 PST 2010
dsimcha wrote:
> == Quote from Fawzi Mohamed (fawzi at gmx.ch)'s article
>> I already thought that having a aassert (always assert) would
>> be nice...
>
> In D2, isn't that what enforce() is for?
It is indeed.
I've found enforce() to be extremely practical. I use it every time I
would otherwise write
if (some test fails) throw new Exception("Some test failed");
Besides being slightly shorter than the above, it saves me the trouble
of providing the file and line number where the error occurred. Also,
its sibling errnoEnforce() is invaluable when working directly with C
functions, where the alternative is
if (some test fails) throw new Exception(to!string(strerror(errno)));
In fact, there are a lot of goodies in std.contracts that I suspect most
people aren't aware of.
-Lars
More information about the Digitalmars-d
mailing list