enforce()?

Ali Çehreli acehreli at yahoo.com
Wed Jun 16 00:14:00 PDT 2010


bearophile wrote:
> I have counted about 200 usages of std.contracts.enforce() inside Phobos. Can you tell me what's the purpose of enforce() in a language that has built-in Contract Programming?

I can see two benefits:

1) enforce throws object.Exception, which is "the root of the exception 
hierarchy"; hence enforce errors can be caught with the same general 
catch(Exception) clause [*].

On the other hand, assert throws a type that is out of the Exception 
hierarchy: core.exception.AssertError

2) As a bonus, the word 'enforce' fits the purpose better than 'assert'

3) (the other 2 :p) The format of the message of the uncaught exceptions 
is a little better (e.g. no @ sign before the file name)

Ali

* Note: Actually, Throwable is at the top of the exception hierarchy, 
but I've heard before that the top exception class should be taken to be 
Exception; perhaps for user applications?


More information about the Digitalmars-d mailing list