[phobos] Pureness of enforce()

Lars Tandle Kyllingstad lars at kyllingen.net
Tue Nov 9 09:16:53 PST 2010


On Tue, 2010-11-09 at 08:48 -0800, Andrei Alexandrescu wrote:
> On 11/9/10 8:34 AM, Don Clugston wrote:
> > On 9 November 2010 16:15, Robert Jacques<sandford at jhu.edu>  wrote:
> >> On Tue, 09 Nov 2010 06:21:38 -0500, Lars Tandle Kyllingstad
> >> <lars at kyllingen.net>  wrote:
> >>>   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.
> >
> >> Well, long term, we need modifiers to apply to delegates. i.e. it should be
> >> possible to declare a pure delegate.
> >
> > You can already do that.
> >
> > int delegate(int x, int y) pure @safe foo;
> 
> Cool! Then overloading enforce on purity should be the solution.
> 
> Andrei

How would you do that?  If you mean defining enforce() like

  void enforce(bool test, string delegate() pure msg) pure
  {
      if (test) throw new Exception(msg());
  }

that would mean that the compiler must be able to implicitly convert a
string parameter to a delegate returning string, as well as
automatically detecting whether that delegate is pure.

  string lastError;
  void main()
  {
      enforce(true, "failure");             // Call pure enforce
      enforce(true, lastError = "failure"); // Call non-pure enforce
  }

-Lars



More information about the phobos mailing list