www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - static functions?

reply Andy Valencia <dont spam.me> writes:
Leveraging my knowledge of C, I assumed a "static" function would 
be hidden outside of its own source file.  I can't find any 
statement about the semantics of a static function in the 
documentation, and in practice (ldc2 on Linux) it doesn't hide 
the function?


file tst.d:

import std.stdio : writeln;
import tst1;

void
main()
{
     writeln(do_op());
     writeln(do_op());
}


and file tst1.d:

static int
do_op()
{
     static int x;

     x += 1;
     return(x);
}
Mar 11
parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Monday, March 11, 2024 9:56:24 AM MDT Andy Valencia via Digitalmars-d-learn 
wrote:
 Leveraging my knowledge of C, I assumed a "static" function would
 be hidden outside of its own source file.  I can't find any
 statement about the semantics of a static function in the
 documentation, and in practice (ldc2 on Linux) it doesn't hide
 the function?
No, static does nothing of the sort in D. You can read the documentation here: https://dlang.org/spec/attribute.html#static But what exactly static means varies based on the context. For module constructors and destructors, it means that that constructor or destructor runs once for the entire program, whereas otherwise, they would run once per thread. For member functions (i.e. functions on structs or classes), it means that the function has no this reference. So, it's a function on the struct/class itself and not associated with an instance of the struct/class, whereas non-static member functions must be called on an instance of that struct or class. For nested functions, it means that the function does not have access to variables inside the function that it's nested in (whereas a non-static member function has access the symbols in the function that it's declared in). For member variables (that is, variables on a struct or class), it makes it so that there is only one instance of that variable for that struct or class per thread rather than one per instance of the struct or class. For variables within a function, it makes it so that there is only one instance of that variable per thread (as opposed to the variable only existing for the duration of a specific function call). For nested structs or classes, it means that they don't have access to their outer scope (generally either the function that they're declared in or the class or struct that they're declared in). I'm probably missing some other uses of static, but those are the ones that come to mind at the moment. Aside from module constructors and destructors, I can't think of any case off the top of my head where static does anything at module scope. So, putting static on a function at module scope (rather than within a struct or class) does nothing. If you want to control the visibility of any symbol within a module, then you need to use the visibility attributes: https://dlang.org/spec/attribute.html#visibility_attributes And specifically, to make a symbol only be visible inside a module, you mark it with private. - Jonathan M Davis
Mar 11
parent reply Andy Valencia <dont spam.me> writes:
On Monday, 11 March 2024 at 16:25:13 UTC, Jonathan M Davis wrote:
 ...
 But what exactly static means varies based on the context.
Thank you for the list! But none of those appear to apply to a function defined in the outermost scope of the module. Is static accepted here--but has no actual effect? I will look at the privacy controls--thanks again. Andy
Mar 11
next sibling parent user1234 <user1234 12.de> writes:
On Monday, 11 March 2024 at 16:51:48 UTC, Andy Valencia wrote:
 On Monday, 11 March 2024 at 16:25:13 UTC, Jonathan M Davis 
 wrote:
 ...
 But what exactly static means varies based on the context.
Thank you for the list! But none of those appear to apply to a function defined in the outermost scope of the module. Is static accepted here--but has no actual effect?
Yes module-level `static` (for what is aka "free-functions", but also variables) is a noop. Module-level is implicitly static.
 I will look at the privacy controls--thanks again.
No need to ;) D `static` is a storage class, not a visibility attribute.
 Andy
Mar 11
prev sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Monday, March 11, 2024 10:51:48 AM MDT Andy Valencia via Digitalmars-d-
learn wrote:
 On Monday, 11 March 2024 at 16:25:13 UTC, Jonathan M Davis wrote:
 ...
 But what exactly static means varies based on the context.
Thank you for the list! But none of those appear to apply to a function defined in the outermost scope of the module. Is static accepted here--but has no actual effect?
There are a number of cases where D allows you to use attributes that are then ignored when they're used on a symbol where they don't make sense. It's particularly useful when using the : syntax, since then you can apply an attribute to the file as a whole without getting a bunch of errors about that attribute not applying to some of the symbols within the module, but it does have the downside of making it less obvious when an attribute is ignored.
 I will look at the privacy controls--thanks again.
Yes, that's what you want if you want to control which symbols are visible outside the module. - Jonathan M Davis
Mar 11