Improvement in pure functions specification

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Tue Dec 20 17:54:37 PST 2016


On Tue, Dec 20, 2016 at 08:16:36PM -0500, Andrei Alexandrescu via Digitalmars-d wrote:
> On 12/20/16 8:02 PM, H. S. Teoh via Digitalmars-d wrote:
> > Isn't that impure by definition?!  How can tracing execution even be
> > remotely considered pure?
> 
> That's why the compiler is required compulsively to call it. -- Andrei

I don't follow.

The question was how can a function with side-effects (even given D's
relaxed definition of pure, i.e., allowing mutation via arguments) can
be considered pure.  What has that got to do with the compiler being
required to call it?

I'd say a pure void function should be equivalent to no-op and elidable.
If you want to call a function with side-effects from pure code for
debugging purposes, call it via the debug statement, e.g.:

	void impureDebugger(...) { ... } // N.B.: NOT pure

	pure void noop() {}

	pure auto pureFunc(A...)(A args) {
		...
		noop();	// may be elided
		...
		debug impureDebugger(1,2,3); // won't be elided
		...
	}

I don't see any good reason why we should bend the definition of pure to
allow pure void functions with side effects.


T

-- 
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth


More information about the Digitalmars-d mailing list