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

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sun Oct 22 00:50:33 UTC 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 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





More information about the Digitalmars-d-learn mailing list