enforce()?

Ali Çehreli acehreli at yahoo.com
Wed Jun 16 12:28:39 PDT 2010


Jonathan M Davis wrote:

 > Assertions assert that something is _always_ true and that if it 
isn't, the
 > program is wrong, while exceptions are for exceptional circumstances

Makes sense.

 > You use [enforce] when you want an exception thrown rather than when 
you want to
 > kill your program due to an error.

To further confuse the issue, assert throws too:

import std.stdio;
import std.algorithm;

void main()
{
     try {
         assert(false);
     } catch (Throwable) {
         writeln("an assertion failed");
     }
}

The difference is just the exception that is thrown. Throwable seems to 
be most general.

 From what I've read so far, I take enforce as a replacement to what it 
exactly is:

if (condition) {
     throw /* ... */;
}

Since I never use assert for that purpose, I take enforce as a shortcut 
for the above.

Ali


More information about the Digitalmars-d mailing list