View mode: basic / threaded / horizontal-split · Log in · Help
December 02, 2007
static class Foo {}
Hi,

I like to ask of the meaning of
static class Foo {}
I thought I could use it like the C++ namespace keyword.
But static doesn't seem to have an effect.
Things that do not have an effect should result in compiler
warnings or even errors.
December 02, 2007
Re: static class Foo {}
mandel:
> I thought I could use it like the C++ namespace keyword.

D has modules and packages as Python, use them.


> Things that do not have an effect should result in compiler
> warnings or even errors.

I may agree, usually.

Bye,
bearophile
December 02, 2007
Re: static class Foo {}
"mandel" <oh@no.es> wrote in message news:fiumjg$2uol$2@digitalmars.com...
> Hi,
>
> I like to ask of the meaning of
> static class Foo {}
> I thought I could use it like the C++ namespace keyword.
> But static doesn't seem to have an effect.
> Things that do not have an effect should result in compiler
> warnings or even errors.

Inside another class, static classes are classes without outer pointers.

class A
{
   int x;

   class B
   {
       void foo()
       {
           writefln(x); // works fine, accesses this.outer.x
       }
   }

   static class C
   {
       void bar()
       {
           writefln(x); // error, 'this' for 'x' needs to be of type 'A', 
not 'C'
       }
   }
}

Outside, static does nothing.

It's annoying that redundant attributes/protection specifiers are allowed by 
the compiler, but it supposedly makes generic code easier, mostly when 
dealing with mixins.
December 02, 2007
Re: static class Foo {}
bearophile wrote:

> D has modules and packages as Python, use them.
Local namespaces have their purpose as
nested classes and functions do have.
Having files/modules for them is a blunt instrument.

I thought of

static class Foo
{
 uint a;
 uint b;
}

as a shortcut for smth. similar to

class Foo
{
 static uint a;
 static uint b;
}

But with the difference that a "static class" wouldn't allow to be 
instanciated (singleton out fo the box).
December 02, 2007
Re: static class Foo {}
Jarrett Billingsley wrote:
> 
> Inside another class, static classes are classes without outer pointers.
Ok, that's behaviour I expected.
But not quite as I hoped regarding to the outside declaration.

[..]
> 
> Outside, static does nothing.
Top | Discussion index | About this forum | D home