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

Lars T. Kyllingstad public at kyllingen.NOSPAMnet
Thu Mar 17 03:09:50 PDT 2011


On Thu, 17 Mar 2011 07:51:33 +0100, 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.

You can overload on pure, yes, but only when you pass an explicit 
delegate.  The following doesn't work, for instance:

  void foo(pure lazy string s) { ... }

The compiler complains that "basic type expected, not pure".

With "lazy", the compiler creates a delegate under the hood, and it needs 
the ability to deduce when that delegate can be marked as pure.  For the 
function above, for instance, passing a simple string literal should be 
perfectly fine, as should the following:

  int baz() pure { ... }
  string bar(int i) pure { ... }

  foo(bar(baz()));

-Lars


More information about the Digitalmars-d mailing list