www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Static function at module level

reply "Phil Lavoie" <maidenphil hotmail.com> writes:
Ok, so after years of D usage I just noticed that this is valid D 
(compiles anyways):

static void myFunc() {}

What is a static function at module level exactly? In C, that 
means private, in D, that means ___________?

Thanks,
Phil
Aug 17 2014
next sibling parent reply Jonathan M Davis via Digitalmars-d-learn writes:
On Mon, 18 Aug 2014 01:32:40 +0000
Phil Lavoie via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

 Ok, so after years of D usage I just noticed that this is valid D
 (compiles anyways):

 static void myFunc() {}

 What is a static function at module level exactly? In C, that
 means private, in D, that means ___________?
I'm pretty sure that it means nothing. It's just one of those cases where an attribute is ignored, because it doesn't apply rather than resulting in an error. - Jonathan M Davis
Aug 17 2014
parent "Phil Lavoie" <maidenphil hotmail.com> writes:
On Monday, 18 August 2014 at 05:29:53 UTC, Jonathan M Davis via 
Digitalmars-d-learn wrote:
 On Mon, 18 Aug 2014 01:32:40 +0000
 Phil Lavoie via Digitalmars-d-learn 
 <digitalmars-d-learn puremagic.com>
 wrote:

 Ok, so after years of D usage I just noticed that this is 
 valid D
 (compiles anyways):

 static void myFunc() {}

 What is a static function at module level exactly? In C, that
 means private, in D, that means ___________?
I'm pretty sure that it means nothing. It's just one of those cases where an attribute is ignored, because it doesn't apply rather than resulting in an error. - Jonathan M Davis
All right thanks!
Aug 18 2014
prev sibling parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Mon, 18 Aug 2014 01:32:40 +0000
Phil Lavoie via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

 What is a static function at module level exactly? In C, that=20
 means private, in D, that means ___________?
nothing. static attribute for free function has no special meaning. but other function declarations (methods, nested functions) accepts 'static', so why free functions shouldn't?
Aug 17 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
ketmar:

 other function declarations (methods, nested functions) accepts
 'static', so why free functions shouldn't?
For various reasons, one of them is that accepting useless code confuses newbies and doesn't allow them to build a correct model of the D semantics in their head. Bye, bearophile
Aug 17 2014
next sibling parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Mon, Aug 18, 2014 at 06:46:02AM +0000, bearophile via Digitalmars-d-learn
wrote:
 ketmar:
 
other function declarations (methods, nested functions) accepts
'static', so why free functions shouldn't?
For various reasons, one of them is that accepting useless code confuses newbies and doesn't allow them to build a correct model of the D semantics in their head.
[...] Is there a bug filed for this? T -- Век живи - век учись. А дураком помрёшь.
Aug 18 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
H. S. Teoh:

 Is there a bug filed for this?
Probably there is. But I stopped filing similar bugs because they seem to have a very low priority. Bye, bearophile
Aug 18 2014
parent "Phil Lavoie" <maidenphil hotmail.com> writes:
On Monday, 18 August 2014 at 14:23:47 UTC, bearophile wrote:
 H. S. Teoh:

 Is there a bug filed for this?
Probably there is. But I stopped filing similar bugs because they seem to have a very low priority. Bye, bearophile
I looked around for it but didn't find it. I filed this one: https://issues.dlang.org/show_bug.cgi?id=13322 As you'll see, every type of module level declaration accepts static. static int x; static void myFunc(){} static interface MyInterface{} static class MyClass{} static struct MyStruct{} static template myTemplate{} And maybe I am missing some.
Aug 18 2014
prev sibling next sibling parent "Phil Lavoie" <maidenphil hotmail.com> writes:
On Monday, 18 August 2014 at 06:46:03 UTC, bearophile wrote:
 ketmar:

 other function declarations (methods, nested functions) accepts
 'static', so why free functions shouldn't?
For various reasons, one of them is that accepting useless code confuses newbies and doesn't allow them to build a correct model of the D semantics in their head. Bye, bearophile
Agreed. I was misled for a minute. I don't think it should compile.
Aug 18 2014
prev sibling parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Mon, 18 Aug 2014 06:46:02 +0000
bearophile via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

 accepting useless code confuses newbies
i think that i'm not really a newbie now ;-), but i'm still used to declare various private module functions and variables as 'static'. yes, sometimes this confuses me (as to "do i need to make this sta... ah, scrap that, it's D!"), but sometimes this is handy. why? i'm still have to use C sometimes, and i'm writing 'static' automatically. having compiler to accept it for anything high-level saves me one regexp search-and-replace. ;-)
Aug 18 2014
parent reply "Phil Lavoie" <maidenphil hotmail.com> writes:
On Monday, 18 August 2014 at 17:42:37 UTC, ketmar via 
Digitalmars-d-learn wrote:
 On Mon, 18 Aug 2014 06:46:02 +0000
 bearophile via Digitalmars-d-learn 
 <digitalmars-d-learn puremagic.com>
 wrote:

 accepting useless code confuses newbies
i think that i'm not really a newbie now ;-), but i'm still used to declare various private module functions and variables as 'static'. yes, sometimes this confuses me (as to "do i need to make this sta... ah, scrap that, it's D!"), but sometimes this is handy. why? i'm still have to use C sometimes, and i'm writing 'static' automatically. having compiler to accept it for anything high-level saves me one regexp search-and-replace. ;-)
I don't think he meant you personally. Well, I hope not. I was confused by it too and I don't consider myself a D newbie. I get that it is convenient for you. I have done a lot of C myself. However, convenience loses to misleading in my book. Consider that in the future, for example, "static interface Toto{}" means something different than "interface Toto{}". I am not debating whether or not that would ever happen or what would even be the meaning of a static interface (even though I have an idea), the point is more like this: every compiler version will accept both versions of said interface, but some of those compiler will interpret it differently. Now that's a problem. Philz
Aug 18 2014
parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Mon, 18 Aug 2014 17:55:01 +0000
Phil Lavoie via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

 I don't think he meant you personally.
ah, nothing personal, i just described my use case, maybe using some vague wording. i AM a newbie in D, there is nothing insulting in being newbie. ;-)
 the point is more like this: every compiler version will=20
 accept both versions of said interface, but some of those=20
 compiler will interpret it differently. Now that's a problem.
hm. to be honest, i just don't want to fix almost all my code removing spurious 'static's here and there. ;-)
Aug 18 2014