enforce()?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Jun 16 07:53:12 PDT 2010


Leandro Lucarella wrote:
> Steven Schveighoffer, el 16 de junio a las 06:55 me escribiste:
>> On Wed, 16 Jun 2010 05:28:46 -0400, Ary Borenszweig
>> <ary at esperanto.org.ar> wrote:
>>
>>> On 06/16/2010 04:15 PM, Walter Bright wrote:
>>>> Ali Çehreli wrote:
>>>>> 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:
>>>> The difference is not based on those 3 points, but on what Andrei wrote
>>>> here. Contracts and error checking are completely distinct activities
>>>> and should not be conflated.
>>> Could you please explain them? There are many people here that
>>> don't understand the difference between these two concepts
>>> (including me). So maybe we are too dumb, maybe those concepts are
>>> not generally known or maybe the explanation is not very well
>>> clear in the documentation.
>> I think of enforce as a convenient way translating an error in an
>> expectation to an exception in a single expression.
>>
>> For example, take some system call that returns -1 on error, you
>> could do this:
>>
>> if(result < 0)
>>    throw new Exception("oops!");
>>
>> or you could do this:
>>
>> enforce(result >= 0, "oops!");
>>
>> Think of enforce as "throw if"
> 
> So maybe throw_if() would be a better name =)
> 
> Anyway, I think enforce() is poisson,

Indeed it is a bit fishy :o).

> because it make the programmer to
> not think about errors at all, just add and enforce() and there you go.
> But when you need to be fault tolerant, is very important to know what's
> the nature of the error, but thanks to enforce(), almost every error is
> a plain Exception, no hierarchy, no extra info, all you can do to get
> a little more info about what happened is to parse the exception string,
> and that's not really an option.

I think there is no real need for exception hierarchies. I occasionally 
dream of eliminating all of the useless exceptions defined left and 
right in Phobos.

>> And in fact, I think there's an errnoEnforce which throws a standard
>> exception with the string error from the system.
> 
> That's the only useful case of enforce, because it includes the
> *important* information (the actual errno).
> 
> There is also enforceEx!(), to use a custom exception, which practically
> nobody uses (I counted only 4 uses in phobos).

I'd be hard pressed to find good examples of exception hierarchy use. 
Everybody talks about them but I've seen none.

The fact that the coder doesn't need to think hard to use enforce() 
effectively is a plus, not a minus. An overdesigned enforce that adds 
extra burden to its user would have been a mistake.


Andrei


More information about the Digitalmars-d mailing list