August 22, 2023
https://issues.dlang.org/show_bug.cgi?id=24100

          Issue ID: 24100
           Summary: proposal to implement "partially pure" functions
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody@puremagic.com
          Reporter: andreiD@yopmail.com

proposal to implement "partially pure" functions
(in the footsteps of Issue 24096)

Rationale:
1. Expand the range of access restrictions to arbitrary program data.

2. Make a pure function deterministic.


  usage relationship diagram.

           no pure
         /    |    \
 pure(in) pure(out) pure(ref)
        \     |    /      \
             pure         I\O



A possible syntax for defining a "partially pure" function is:

pure(in) type fun(...);  //does not change external variables
pure(out) type fun(...); //not changed by external variables
pure(ref) type fun(...);  //can use I/O functions but cannot use
pure(in)/pure(out) functions

example:

int a;

void f(){int x = a;} //ok
void f(){int x; a = x;} //ok
void f(){writeln("no ok"} //ok

pure(in) void f(){int x = a;} // ok
pure(in) void f(){int x; a = x;} //error
pure(in) void f(){writeln("no ok"} // error

pure(out) void f(){int x; a = x;} // ok
pure(out) void f(){int x = a;} //error
pure(out) void f(){writeln("no ok"} // error

pure(ref) void f(){ writeln("ok");} // ok
pure(ref) void f(){int x = a;} // error
pure(ref) void f(){int x; a = x;} //error

pure void f(){int x = a;} // error
pure void f(){int x; a = x;} //error
pure void f(){writeln("no ok"} // error


pure(in) -  can be thought of as a closure.
pure(out) - can be comprehended as a method/subprogram.
pure(ref) - can be thought of as a non-monad IO / function for resource
management.

--