www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4031] New: Tidier management of static variables in pure functions

reply d-bugmail puremagic.com writes:
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



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: -------
Mar 30 2010
next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Jul 02 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4031


Walter Bright <bugzilla digitalmars.com> changed:

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



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: -------
Jul 03 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4031




Thank you both for fixing the first thing.


 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: -------
Jul 03 2011
prev sibling parent d-bugmail puremagic.com writes:
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



This was fixed a while ago.

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