Against enforce()

bearophile bearophileHUGS at lycos.com
Wed Mar 16 19:33:19 PDT 2011


Andrei:

> No need to blow out of proportion everything that serves a point.

You are right, but probably I am doing that because I see you nearly deaf to the problems I see in  Phobos usages of enforce().


> There are only few places in which use of enforce (or checking in general) 
> slows things down.

I have hit some of such cases in my code, finding them through profiling. At first I didn't expect Phobos functions to give those problems :-(


> Because I don't condone defining large exception hierarchies.

The WrongArgumentException I am talking about comes from a set of less than about ten most useful exceptions, and I mean this hierarchy to be flat, all of them come from Exception. So this is not a large exception hierarchy.


> but that's not an issue that is the charter of enforce or that 
> enforce prevents.

When you design a language and its standard library you have to keep a balance between making things very easy but not good enough, and very hard/fussy but better (see the simplicity of ddoc and D unittests, ddoc is mostly OK, but unnittests are probably a bit too much simple, they miss some essential features like a name). In my opinion a standard library is meant to throw a bit better exceptions than fully generic ones.


> It makes the standard library writers productive.

enforce() has clearly some disadvantages. I believe the very small convenience it brings to Phobos writers is not enough to justify its usage in many cases. One of such cases is inside iota(), where I suggest to replace its usage with a if+throw to allow iota() to be usable in pure functions too.


> Hoping less and doing more would be great.

You are right. I am sorry. I am trying to help, even if I am not doing enough.

Bye and thank you for your answers,
bearophile


More information about the Digitalmars-d mailing list