Against if (!expression) throw new Exception(msg);

Jonathan M Davis jmdavisProg at gmx.com
Thu Mar 17 00:13:22 PDT 2011


On Wednesday 16 March 2011 23:51:33 Don wrote:
> Daniel Gibson wrote:
> > I'd just like to summarize what useful stuff came out of this topic:
> > 
> > * enforce is useful and more than a "always activated assert" ;-)
> > * enforce prevents inlining and thus has negative impact on performance -
> > this should probably be fixed.
> > * enforce can't be used in weakly pure functions - this has the
> > side-effect that iota() (maybe other functions as well) can't be used in
> > pure functions  - this should also be fixed. if fixing it is difficult
> > /maybe/ if+throw should be used in potentially (weakly) pure functions
> > in phobos until it is fixed
> 
> That was discussed on the Phobos ng some time back. I don't think it's a
> compiler issue. It's just not pure because the lazy delegate isn't
> marked as pure. Since you can overload on pure, I'm not sure why this
> hasn't been done yet.

Probably because

1. Very little has been done with pure in Phobos thus far in general.

2. How many people know that you can overload on pure? _I_ sure didn't know 
that. How does that even work? The pure version is called if the caller is pure? 
Or are you talking about overloading on the lazy delegate? How would that be 
determined to be pure anyway? If the expression is pure? The fact that format 
isn't pure will rain on that parade a fair bit, but  there _are_ quite a few 
uses of enforce (probably even the majority) which are just string literals, so 
that would go a fair ways.

Overall, I think that the biggest obstacle to pure at this point is the lack of 
conditional purity for templates. We need to be able to say that a templated 
function is pure if it meets some set of conditions (like every function that it 
calls is pure). I believe that the best proposal for syntax on that is to do 
pure(condition) instead of just pure. Of course, nothrow, @safe, @trusted, etc. 
would likely need to have the same. But regardless, the fact that you can't 
generally make templated functions pure (especially in std.range and 
std.algorithm) _really_ puts a kink in using purity.

I _was_ able to use pure fairly heavily in std.datetime, but it's probably just 
about the only place in Phobos right now that use pure much.

- Jonathan M Davis


More information about the Digitalmars-d mailing list