[phobos] Pureness of enforce()

Robert Jacques sandford at jhu.edu
Tue Nov 9 07:15:10 PST 2010


On Tue, 09 Nov 2010 06:21:38 -0500, Lars Tandle Kyllingstad  
<lars at kyllingen.net> wrote:

> enforce(x) is basically a substitute for
>
>   if (x) throw new Exception;
>
> which doesn't violate the purity rules.  It is not possible to annotate
> enforce() with 'pure', however, because it takes a lazy parameter, which
> is just shorthand for a (possibly impure) delegate.
>
> enforce() is used virtually everywhere in Phobos, and this means that a
> lot of functions that could otherwise be marked as pure, currently
> can't.
>
> One example is std.conv.to(), which should definitely be pure.  (In
> fact, I tried marking it as such, which is what got me thinking about
> this in the first place.)
>
> What to do?
>
> -Lars

Well, long term, we need modifiers to apply to delegates. i.e. it should  
be possible to declare a pure delegate. (Or a const delegate. Or an  
immutable delegate. Or a shared delegate, etc.) For example, right now,  
it's not possible to (type-) safely use a delegate in shared code. And  
David's std.parallelism is a prime case where many methods could be marked  
@safe instead of @here_be_dragons if delegate modifiers existed.


More information about the phobos mailing list