Thread overview
[Issue 4031] New: Tidier management of static variables in pure functions
[Issue 4031] Should be able to access const value-type globals from pure functions
Jul 02, 2011
yebblies
Jul 04, 2011
Walter Bright
Feb 02, 2012
yebblies
March 30, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4031

           Summary: Tidier management of static variables in pure
                    functions
           Product: D
           Version: future
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: accepts-invalid, rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2010-03-30 03:00:41 PDT ---
Here I propose two small different changes: to refuse line 2 and accept line 4.

Mutable static variables can't be used in pure functions, so it's better to really disallow them, and produce a compile error at line 2 here (there's no point in allowing their definition and disallowing just their usage).

(On the other hand the static "z" at line 3 is a const and can be allowed, as
dmd does now.)

The compiler can also allow the line 4, because z will not change, keeping the function foo pure (there is no point in disallowing something that's semantically correct and safe).


pure int foo(int x) {
    static int y = 10;       // line 2  ERR
    const static int z = 20; // line 3  OK (as now)
    return x + z;            // line 4  OK
}
void main() {}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 02, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4031


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|accepts-invalid             |patch
                 CC|                            |yebblies@gmail.com
            Summary|Tidier management of static |Should be able to access
                   |variables in pure functions |const value-type globals
                   |                            |from pure functions


--- Comment #1 from yebblies <yebblies@gmail.com> 2011-07-03 05:53:47 EST ---
https://github.com/D-Programming-Language/dmd/pull/194

This allows static and global value type variables to be used in pure functions.

I'm not sure if disabling mutable static variables inside pure functions is valid, as some actions on them (eg. returning their address) do make sense. They can also be accessed from any statements inside debug {}.  Please open a new report for that if you would like it.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 04, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4031


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com


--- Comment #2 from Walter Bright <bugzilla@digitalmars.com> 2011-07-03 17:25:47 PDT ---
https://github.com/D-Programming-Language/dmd/commit/b135734828b239e52ea95f34a59081b526d9fa5e

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 04, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4031



--- Comment #3 from bearophile_hugs@eml.cc 2011-07-03 17:54:44 PDT ---
Thank you both for fixing the first thing.

(In reply to comment #1)
> I'm not sure if disabling mutable static variables inside pure functions is valid, as some actions on them (eg. returning their address) do make sense.

Returning the address of a static variable defined inside a pure function? I don't see the purpose of this. It smells.


> They can also be accessed from any statements inside debug {}.

But this need is probably uncommon, and when this needs arises you are able to move the definition of such variable inside the debug:


pure int foo(int x) {
    debug {
        static int y = 10;
    }
    return x;
}
void main() {}



> Please open a new report for that if you would like it.

Maybe Walter is willing to comment on this, but I think I will open another report for this.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 02, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4031


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|patch                       |pull
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


--- Comment #4 from yebblies <yebblies@gmail.com> 2012-02-02 12:54:48 EST ---
This was fixed a while ago.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------