www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - dlang.org/spec/function.html#pure-functions example

reply Paul <phshaffer gmail.com> writes:
The spec doc has the following statement and corresponding 
example:
***"Pure functions cannot directly access global or static 
mutable state."***

```d
int x;
immutable int y;

pure int foo(int i)
{
     i++;     // ok, modifying local state
     //x = i; // error, modifying global state
     //i = x; // error, reading mutable global state
     i = y;   // ok, reading immutable global state
     throw new Exception("failed"); // ok
}
```
If **int x** is global mutable state, what does static mutable 
state look like?
Oct 12 2023
next sibling parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Thursday, October 12, 2023 1:33:32 PM MDT Paul via Digitalmars-d-learn 
wrote:
 The spec doc has the following statement and corresponding
 example:
 ***"Pure functions cannot directly access global or static
 mutable state."***

 ```d
 int x;
 immutable int y;

 pure int foo(int i)
 {
      i++;     // ok, modifying local state
      //x = i; // error, modifying global state
      //i = x; // error, reading mutable global state
      i = y;   // ok, reading immutable global state
      throw new Exception("failed"); // ok
 }
 ```
 If **int x** is global mutable state, what does static mutable
 state look like?
Types can have static members. Basically what it comes down to is that outside of immutable data, pure functions only have access to their arguments and to what they can access via their arguments (be it by getting pointers from those arguments or calling other pure functions on them). - Jonathan M Davis
Oct 12 2023
next sibling parent Paul <phshaffer gmail.com> writes:
On Thursday, 12 October 2023 at 21:20:44 UTC, Jonathan M Davis 
wrote:

Thanks Jonathan
Oct 16 2023
prev sibling parent reply Paul <phshaffer gmail.com> writes:
On Thursday, 12 October 2023 at 21:20:44 UTC, Jonathan M Davis 
wrote:
 look like?

 Types can have static members.

 Basically what it comes down to is that outside of immutable 
 data, pure functions only have access to their arguments and to 
 what they can access via their arguments (be it by getting 
 pointers from those arguments or calling other pure functions 
 on them).

 - Jonathan M Davis
Can I say in the general sense that when the word static is used it means that something is defined/declared at compile time?
Oct 16 2023
next sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Monday, October 16, 2023 12:05:04 PM MDT Paul via Digitalmars-d-learn 
wrote:
 On Thursday, 12 October 2023 at 21:20:44 UTC, Jonathan M Davis
 
 wrote:
 look like?
 
 Types can have static members.
 
 Basically what it comes down to is that outside of immutable
 data, pure functions only have access to their arguments and to
 what they can access via their arguments (be it by getting
 pointers from those arguments or calling other pure functions
 on them).
 
 - Jonathan M Davis
Can I say in the general sense that when the word static is used it means that something is defined/declared at compile time?
Oct 21 2023
prev sibling next sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Monday, October 16, 2023 12:05:04 PM MDT Paul via Digitalmars-d-learn 
wrote:
 On Thursday, 12 October 2023 at 21:20:44 UTC, Jonathan M Davis

 wrote:
 look like?

 Types can have static members.

 Basically what it comes down to is that outside of immutable
 data, pure functions only have access to their arguments and to
 what they can access via their arguments (be it by getting
 pointers from those arguments or calling other pure functions
 on them).

 - Jonathan M Davis
Can I say in the general sense that when the word static is used it means that something is defined/declared at compile time?
Hmmm. It seems like my message got eaten. So, I'll write it out again. In any case, no, in general, static really doesn't have much to with runtime vs compile time. It means different things in different contexts. Off the top of my head, the only contexts where static specifically has anything to do with compile time are with static if and static foreach, in which case, those constructs become compile-time constructs instead of runtime constructos. Other contexts have very different meanings for static. For instance, a static member function is a member function that doesn't have an implicit this reference/pointer and thus is pretty much just a function that's scoped to the class/struct rather than being a function that operates on instances of that class or struct. On the other hand, static member variables are variables which are associated with the class or struct and not with an instance of that class or struct. So, there is only one instance of that variable for all objects of that class or struct on a single thread, as opposed to non-static member variables which are specific to each object. static in functions has similar but different meanings. On a nested function, it makes it so that the function has no implicit parameter which is a reference to the context of the outer function, meaning that it's pretty much just a function within another function, whereas a non-static nested function actually has access to the outer function's scope and thus can access the variables in the outer scope. On the other hand, a static variable within a function is a variable where there is only one instance of that variable for every call to that function on a single thread, as opposed to normal function variables which get a new instance every time that the function is called. And there are other meanings for static in other contexts. There are similarities between them, but if there is a definition that can be given for what static means which covers all of those contexts (and there may be - C manages that in spite of the fact that static means very different things in different contexts there too), it's not an obvious definition. You mostly just have to learn what static means in each context that it's used rather than memorizing a general definition for it that can be applied in each context. - Jonathan M Davis
Oct 21 2023
prev sibling parent Nick Treleaven <nick geany.org> writes:
On Monday, 16 October 2023 at 18:05:04 UTC, Paul wrote:
 On Thursday, 12 October 2023 at 21:20:44 UTC, Jonathan M Davis 
 wrote:
 look like?

 Types can have static members.

 Basically what it comes down to is that outside of immutable 
 data, pure functions only have access to their arguments and 
 to what they can access via their arguments (be it by getting 
 pointers from those arguments or calling other pure functions 
 on them).

 - Jonathan M Davis
Can I say in the general sense that when the word static is used it means that something is defined/declared at compile time?
This link should describe all the uses of `static`: https://dlang.org/spec/attribute.html#static
Oct 28 2023
prev sibling parent Nick Treleaven <nick geany.org> writes:
On Thursday, 12 October 2023 at 19:33:32 UTC, Paul wrote:
 If **int x** is global mutable state, what does static mutable 
 state look like?
In addition to Jonathan's reply, see: https://dlang.org/spec/function.html#local-static-variables
Oct 13 2023