www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Inferring Purity Woes

reply "Xinok" <xinok live.com> writes:
I've been avoiding purity in D for a while but I decided to take 
a stab at it today. I encountered two issues which make inferring 
purity difficult.


(1) While the compiler infers purity for instantiated functions, 
it's unable to infer purity for "functions within templates". 
Consider the following three statements:

void foo()(){ }
template T(){  void foo()(){ }  }
template T(){  void foo(){ }  }

The compiler can infer purity for the first two statements but 
not the third. So even though the functions are within a 
template, you still have to make each individual function an 
"instantiated function" in order to infer purity for them.

I think the compiler should infer purity for all functions, but 
adding support for functions within templates would be good 
enough. Otherwise, you end up with a lot of syntactic noise by 
adding empty parenthesis to all of these function declarations 
without any clear reason why they're even there.


(2) When the compiler fails to infer purity, it prints an error 
"pure function [] cannot call impure function []". However, it 
fails to tell you where the impurity occurs. So if you have a 
series of "instantiated functions" spanning thousands of lines of 
code, you have to manually sift through all of that code to find 
the impurity.


Are there any bug reports for these two issues? I tried searching 
but couldn't find anything.
May 15 2015
next sibling parent "weaselcat" <weaselcat gmail.com> writes:
On Saturday, 16 May 2015 at 02:30:52 UTC, Xinok wrote:
 I've been avoiding purity in D for a while
purity is probably one of my favorite parts of D.
 I think the compiler should infer purity for all functions,
anything short of this is just a bandaid. There was a discussion a ~week ago about this in the NG if you want to go hunting.
May 16 2015
prev sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 5/15/15 10:30 PM, Xinok wrote:
 I've been avoiding purity in D for a while but I decided to take a stab
 at it today. I encountered two issues which make inferring purity
 difficult.


 (1) While the compiler infers purity for instantiated functions, it's
 unable to infer purity for "functions within templates". Consider the
 following three statements:

 void foo()(){ }
 template T(){  void foo()(){ }  }
 template T(){  void foo(){ }  }

 The compiler can infer purity for the first two statements but not the
 third. So even though the functions are within a template, you still
 have to make each individual function an "instantiated function" in
 order to infer purity for them.
This is a bug I think. functions that are part of templates should be inferred. It works for structs, e.g.: struct S() { void foo() {} } void main() pure { S!().init.foo(); } -Steve
May 18 2015