Thread overview | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 05, 2005 Private declaration in module not private | ||||
---|---|---|---|---|
| ||||
Is it possible to protect a class or function at module-scope with the private attribute? It would be handy to hide helper classes and functions within modules. I tried declaring a private class and a bunch of functions in a private scope block, but they remain accessible to other modules when I compile with DMD 0.137. Regards, Garett |
November 05, 2005 Re: Private declaration in module not private | ||||
---|---|---|---|---|
| ||||
Posted in reply to Garett Bass | Garett Bass wrote:
> Is it possible to protect a class or function at module-scope with the private attribute? It would be handy to hide helper classes and functions within modules. I tried declaring a private class and a bunch of functions in a private scope block, but they remain accessible to other modules when I compile with DMD 0.137.
>
> Regards,
> Garett
>
>
I tried to bring this up before .. doesn't seem like it's gonna change!
I'd love to hear Walter's opinion on this.
|
November 05, 2005 Re: Private declaration in module not private | ||||
---|---|---|---|---|
| ||||
Posted in reply to Garett Bass | "Garett Bass" <garettbass@studiotekne.com> wrote in message news:dkhiud$lt5$1@digitaldaemon.com... > Is it possible to protect a class or function at module-scope with the private attribute? It would be handy to hide helper classes and functions within modules. I tried declaring a private class and a bunch of functions in a private scope block, but they remain accessible to other modules when I compile with DMD 0.137. I think the whole protection attribute system needs to be looked at and made right. Another interesting thing with protection attributes - outer classes can't access provate/protected inner class members, even though they're (obviously) in the same module. I've run into this wall too many times. That, and I can never seem to get package to work right. |
November 05, 2005 Re: Private declaration in module not private | ||||
---|---|---|---|---|
| ||||
Posted in reply to Garett Bass | In article <dkhiud$lt5$1@digitaldaemon.com>, Garett Bass says... > >Is it possible to protect a class or function at module-scope with the private attribute? It would be handy to hide helper classes and functions within modules. I tried declaring a private class and a bunch of functions in a private scope block, but they remain accessible to other modules when I compile with DMD 0.137. '... accessible to other modules'? Can you post an example? It sounds like you may be speaking of two different things - 'private' declarations are intended to be private to a module, which seems to be the issue you are talking about at first, but it sounds like things are broken in your last sentence. Or, are you talking about making a class definition private: file1.d: private class C { int i = 10; } private { int _i; int foo() { return 10; } } private: C _c; file2.d: import file1; void main() { C c = new C; // Ok printf("%d\n",c.i); // Ok int i = foo(); // should be an error _i = 10; // should be an error _c = new C; // should be an error } Making a class definition (as opposed to a declaration) 'private' is undefined but currently not an error, I think. > >Regards, >Garett > > |
November 05, 2005 Re: Private declaration in module not private | ||||
---|---|---|---|---|
| ||||
Posted in reply to Garett Bass | On Sat, 5 Nov 2005 00:19:23 -0600, Garett Bass <garettbass@studiotekne.com> wrote:
> Is it possible to protect a class or function at module-scope with the private attribute? It would be handy to hide helper classes
> and functions within modules. I tried declaring a private class and a bunch of functions in a private scope block, but they remain
> accessible to other modules when I compile with DMD 0.137.
You can make the constructor and all static methods private, I believe then no other module will be able to instantiate that class nor call any methods.
eg.
class Foo
{
private this() {}
private static void bar() {}
}
Regan
|
November 07, 2005 Re: Private declaration in module not private | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | In article <opszrrz4t823k2f5@nrage.netwin.co.nz>, Regan Heath says... > >On Sat, 5 Nov 2005 00:19:23 -0600, Garett Bass <garettbass@studiotekne.com> wrote: >> Is it possible to protect a class or function at module-scope with the >> private attribute? It would be handy to hide helper classes >> and functions within modules. I tried declaring a private class and a >> bunch of functions in a private scope block, but they remain >> accessible to other modules when I compile with DMD 0.137. > >You can make the constructor and all static methods private, I believe then no other module will be able to instantiate that class nor call any methods. > >eg. > >class Foo >{ > private this() {} > private static void bar() {} >} > However, if you could just do: private class Foo { this() {} static void bar() {} } and have the "same" result, that'd be really NICE. I put "same" because with that workaround you are still able to declare references to Foo objects, like: Foo f; // It's ugly even though you can't instantiate a Foo object. Tom |
November 07, 2005 Re: Private declaration in module not private | ||||
---|---|---|---|---|
| ||||
Posted in reply to Garett Bass | In article <dkhiud$lt5$1@digitaldaemon.com>, Garett Bass says... [...] >I tried declaring a private class I agree, private classes don't work for me neither. >and a bunch of functions in a private scope block, but they remain accessible to other modules when I compile with DMD 0.137. With DMD 0.137, when I declare a function inside a private block, it remains private for other modules, at least for my code. >Regards, >Garett Tom |
November 07, 2005 Re: Private declaration in module not private | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomás Rossi | > With DMD 0.137, when I declare a function inside a private block, it remains private for other modules, at least for my code.
Tom,
It gets interesting here...
------------
module test; // test.d
private void foo() { writefln("private test.foo()"); }
------------
module main; // main.d
import test;
int main(char[][] args) {
//foo(); // Error: "module main test.foo is private"
test.foo(); // OK, prints: "private test.foo()"
return 0;
}
------------
Do you think this is the intended behavior? I'm not sure that it is wrong, but I do find it surprising.
Regards,
Garett
|
November 07, 2005 Re: Private declaration in module not private | ||||
---|---|---|---|---|
| ||||
Posted in reply to Garett Bass | On Mon, 7 Nov 2005 17:27:37 -0600, Garett Bass <garettbass@studiotekne.com> wrote:
>> With DMD 0.137, when I declare a function inside a private block, it remains
>> private for other modules, at least for my code.
>
> Tom,
>
> It gets interesting here...
>
> ------------
>
> module test; // test.d
>
> private void foo() { writefln("private test.foo()"); }
>
> ------------
>
> module main; // main.d
> import test;
>
> int main(char[][] args) {
> //foo(); // Error: "module main test.foo is private"
> test.foo(); // OK, prints: "private test.foo()"
> return 0;
> }
>
> ------------
>
> Do you think this is the intended behavior?
No, I think it's a bug, you should post it to the bugs NG (or I will if you like?)
Regan
|
November 07, 2005 Re: Private declaration in module not private | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | I will submit the bug. "Regan Heath" <regan@netwin.co.nz> wrote in message news:opszv9c1j923k2f5@nrage.netwin.co.nz... > On Mon, 7 Nov 2005 17:27:37 -0600, Garett Bass <garettbass@studiotekne.com> wrote: >>> With DMD 0.137, when I declare a function inside a private block, it remains private for other modules, at least for my code. >> >> Tom, >> >> It gets interesting here... >> >> ------------ >> >> module test; // test.d >> >> private void foo() { writefln("private test.foo()"); } >> >> ------------ >> >> module main; // main.d >> import test; >> >> int main(char[][] args) { >> //foo(); // Error: "module main test.foo is private" >> test.foo(); // OK, prints: "private test.foo()" >> return 0; >> } >> >> ------------ >> >> Do you think this is the intended behavior? > > No, I think it's a bug, you should post it to the bugs NG (or I will if you like?) > > Regan |
Copyright © 1999-2021 by the D Language Foundation