Against enforce()

bearophile bearophileHUGS at lycos.com
Wed Mar 16 18:53:06 PDT 2011


Andrei:

> Yum, love the enforce.

You are silly :-)


> So perhaps the language could be improved as enforce does not break purity.

Currently enforce() uses a lazy argument. A lazy argument is a delegate, and generally such delegate can't be pure, because the expressions you give to enforce() usually refer to variables in the scope where you call enforce(). So what kind of language improvements are you thinking about?

This is an example:
http://d.puremagic.com/issues/show_bug.cgi?id=5746


> > - It kills inlining (with the current DMD, and I don't think this problem will be fixed soon);
> Not a problem of enforce.

In my opinion it's not wise use widely in the standard library something that requires an optimization that the DMD compiler is not going to have soon, and that makes the code significantly slower.
In some cases this almost forces me to keep a patched version of Phobos, or to add more nearly duplicated functions to my dlibs2.


> enforce helps such idioms, does not prevent them. From the docs:
> 
> ===============
> T enforce(T)(T value, lazy Throwable ex);
> 
> If value is nonzero, returns it. Otherwise, throws ex.
> ===============

Then why is iota() using a nude enforce() instead of the enforce() with a more meaningful exception like WrongArgumentException? I have seen the nude enforce used in other parts of Phobos. So maybe enforce() makes even the standard library writers lazy.


> enforce's specification specifies it throws. It would therefore be difficult for it to not throw. This complaint is non sequitur.<

I know, and I agree in some situations you want an assert (to test arguments coming from "outside"), so in some situations an enforce is acceptable. The problem is that currently enforce is used as a patch for a DMD/Phobos problem that I hope will be fixed in a short time, see below.


>> I see enforce() just as a temporary workaround for a problem of Phobos (that it's compiled in release mode, so its asserts are vanished) that risks to become a permanent part of Phobos.<<

> enforce is a simple abstraction of the idiom "if (!condition) throw new Exception(args)". If that idiom were rare, then occurrences of enforce would be rare and therefore there would be little need to have enforce at all.<
>There may be some confusion somewhere. enforce is not supposed to be a sort of assert. It is a different tool with a different charter. Use assert for assertions.<

Right. But saying just that is not enough. You have to add that such "if (!condition) throw new Exception(args)" idiom is common in Phobos because Phobos is present only in release mode. If the zip distribution of DMD contains two Phobos and dmd becomes able to use the right one according to the compilation switches, then I think that "if (!condition) throw new Exception(args)" will become more rare, and the enforce() too will be less commonly needed.

Bye,
bearophile


More information about the Digitalmars-d mailing list