We need a way to make functions pure and/or nothrow based on the

bearophile bearophileHUGS at lycos.com
Sun Nov 14 17:46:59 PST 2010


Jonathan M Davis:

> So, checking the purity of a single function isn't going to do it (which appears to 
> be what your @optional_tag is doing).

The first argument of @optional_tag is a compile-time boolean, so you may test all the functions you want (this reminds me Java checked exceptions a bit):

@optional_tag(isPure!foo && isPure!bar && isPure!spam, pure) int foo(int x) {
  return foo(x) + bar(x) + spam(x);
}


> you need a way to tell the compiler that if every function call within a
> function is pure, then mark it is pure. If no function call within a
> function is nothrow, then mark it as nothrow. 

The 'pure' tag is a way to tell some extra semantics to the compiler, that a function doesn't use global mutable state. This is useful for the programmer because it's simpler to reason about the function (and sometimes it decreases bug count too), and it's useful for the compiler, because in some situations it may optimize more aggressively, memory management too. The "nothrow" tag does something similar for exceptions.

But if you add @optional(pure) you aren't protected against some bugs, because if you call a not pure function your function automatically becomes not pure. The good thing of @optional(pure) is that it's shorter and simpler to use.

Bye,
bearophile


More information about the Digitalmars-d mailing list