digitalmars.D.learn - dlang.org/spec/function.html#pure-functions example
- Paul (18/18) Oct 12 2023 The spec doc has the following statement and corresponding
- Jonathan M Davis (8/26) Oct 12 2023 Types can have static members.
- Paul (3/3) Oct 16 2023 On Thursday, 12 October 2023 at 21:20:44 UTC, Jonathan M Davis
- Paul (4/12) Oct 16 2023 Can I say in the general sense that when the word static is used
- Jonathan M Davis (2/19) Oct 21 2023
- Jonathan M Davis (37/52) Oct 21 2023 Hmmm. It seems like my message got eaten. So, I'll write it out again.
- Nick Treleaven (3/19) Oct 28 2023 This link should describe all the uses of `static`:
- Nick Treleaven (3/5) Oct 13 2023 In addition to Jonathan's reply, see:
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
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
On Thursday, 12 October 2023 at 21:20:44 UTC, Jonathan M Davis wrote: Thanks Jonathan
Oct 16 2023
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 DavisCan 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
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 DavisCan 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
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: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 Davislook 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 DavisCan 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
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:This link should describe all the uses of `static`: https://dlang.org/spec/attribute.html#staticlook 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 DavisCan I say in the general sense that when the word static is used it means that something is defined/declared at compile time?
Oct 28 2023
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