www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Impure static

reply bearophile <bearophileHUGS lycos.com> writes:
This little D2 program, a pure function contains static variables initialized
with calls to not pure functions:


int foo() {
    return 1;
}
pure int bar() {
    enum int x1 = foo();
    static immutable int x2 = foo();
    return x1;
}
void main() {}


The latest DMD gives:

test.d(5): Error: pure function 'bar' cannot call impure function 'foo'
test.d(5):        called from here: foo()
test.d(5):        called from here: foo()
test.d(6): Error: pure function 'bar' cannot call impure function 'foo'
test.d(6):        called from here: foo()
test.d(6):        called from here: foo()

I think I asked a similar question in past, is it right to expect DMD to
compile this program?

Bye and thank you,
bearophile
Nov 09 2011
next sibling parent Tobias Pankrath <tobias pankrath.net> writes:
 
 I think I asked a similar question in past, is it right to expect DMD to
 compile this program?

If foo is CTFE-able, yes.
Nov 09 2011
prev sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 11/09/2011 02:16 PM, bearophile wrote:
 This little D2 program, a pure function contains static variables initialized
with calls to not pure functions:


 int foo() {
      return 1;
 }
 pure int bar() {
      enum int x1 = foo();
      static immutable int x2 = foo();
      return x1;
 }
 void main() {}


 The latest DMD gives:

 test.d(5): Error: pure function 'bar' cannot call impure function 'foo'
 test.d(5):        called from here: foo()
 test.d(5):        called from here: foo()
 test.d(6): Error: pure function 'bar' cannot call impure function 'foo'
 test.d(6):        called from here: foo()
 test.d(6):        called from here: foo()

 I think I asked a similar question in past, is it right to expect DMD to
compile this program?

Yes. Here is the bug report: http://d.puremagic.com/issues/show_bug.cgi?id=6169
Nov 09 2011