www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Templates + protection attributes problem

reply Kevin VR <azra pandora.be> writes:
Hello,

I'm having a problem using templates and the protection attributes for 
class members.
I can not access private declared methods from an other template class 
defined in the same module.

For example when I put this in the same module:
<code>
public class Foo(T) {
	public this() {
		// Works ok.
		Bar!(T) bar = new Bar!(T)();

		// Fails!? it's private, but in the same module!!
		bar.doSomething();
	}
}

public class Bar(T) {
	public this() {
	}
	
	private void doSomething() {
	}
}
</code>

The D-spec says the following:
<quote>
Private means that only members of the enclosing class can access the 
member, or members and functions in the same module as the enclosing class.
</quote>

I'm sure it has something to do with the templates, because the same 
thing without the templates works fine...
Does anyone know how to solve this?

thanks,
Kevin
Apr 06 2005
parent reply "Joey Peters" <squirrel nidhogg.com> writes:
It only works for the same module when you also declare it private within 
the module scope:

private member; <- only for inside this module
class Foo {
 private member; <- only for inside Foo {}
}

What you probably want is the package protection, use the package keyword:

package void doSomething() {}

"Kevin VR" <azra pandora.be> schreef in bericht 
news:d31ndi$amo$1 digitaldaemon.com...
 Hello,

 I'm having a problem using templates and the protection attributes for 
 class members.
 I can not access private declared methods from an other template class 
 defined in the same module.

 For example when I put this in the same module:
 <code>
 public class Foo(T) {
 public this() {
 // Works ok.
 Bar!(T) bar = new Bar!(T)();

 // Fails!? it's private, but in the same module!!
 bar.doSomething();
 }
 }

 public class Bar(T) {
 public this() {
 }

 private void doSomething() {
 }
 }
 </code>

 The D-spec says the following:
 <quote>
 Private means that only members of the enclosing class can access the 
 member, or members and functions in the same module as the enclosing 
 class.
 </quote>

 I'm sure it has something to do with the templates, because the same thing 
 without the templates works fine...
 Does anyone know how to solve this?

 thanks,
 Kevin 
Apr 07 2005
parent reply "Regan Heath" <regan netwin.co.nz> writes:
On Thu, 7 Apr 2005 13:02:33 +0200, Joey Peters <squirrel nidhogg.com>  
wrote:
 It only works for the same module when you also declare it private within
 the module scope:

 private member; <- only for inside this module
 class Foo {
  private member; <- only for inside Foo {}
 }
Actually, no, both of those members above are accessable by another class/function etc in the same module. eg. import std.stdio; class A { private int aival; void foo(B b) { b.bival = 3; } } class B { private int bival; void foo(A a) { a.aival = 4; } } void main() { A a = new A(); B b = new B(); a.aival = 1; b.bival = 2; writefln(a.aival); writefln(b.bival); a.foo(b); b.foo(a); writefln(b.bival); writefln(a.aival); }
 "Kevin VR" <azra pandora.be> schreef in bericht
 news:d31ndi$amo$1 digitaldaemon.com...
 Hello,

 I'm having a problem using templates and the protection attributes for
 class members.
 I can not access private declared methods from an other template class
 defined in the same module.

 For example when I put this in the same module:
 <code>
 public class Foo(T) {
 public this() {
 // Works ok.
 Bar!(T) bar = new Bar!(T)();

 // Fails!? it's private, but in the same module!!
 bar.doSomething();
 }
 }

 public class Bar(T) {
 public this() {
 }

 private void doSomething() {
 }
 }
 </code>

 The D-spec says the following:
 <quote>
 Private means that only members of the enclosing class can access the
 member, or members and functions in the same module as the enclosing
 class.
 </quote>

 I'm sure it has something to do with the templates, because the same  
 thing
 without the templates works fine...
 Does anyone know how to solve this?
Nope. It looks like a bug to me, here is another case demonstrating one that works, and one that doesn't. class Bar(Type) { this() { } private void doSomething() { } } class Baz { this() { } private void doSomething() { } } void main() { //this is ok Baz a = new Baz(); a.doSomething(); //this fails Bar!(int) b = new Bar!(int)(); b.doSomething(); } Regan
Apr 07 2005
parent "Regan Heath" <regan netwin.co.nz> writes:
On Fri, 08 Apr 2005 00:27:37 +1200, Regan Heath <regan netwin.co.nz> wrote:

 On Thu, 7 Apr 2005 13:02:33 +0200, Joey Peters <squirrel nidhogg.com>  
 wrote:
 It only works for the same module when you also declare it private  
 within
 the module scope:

 private member; <- only for inside this module
 class Foo {
  private member; <- only for inside Foo {}
 }
Actually, no, both of those members above are accessable by another class/function etc in the same module. eg. import std.stdio; class A { private int aival; void foo(B b) { b.bival = 3; } } class B { private int bival; void foo(A a) { a.aival = 4; } } void main() { A a = new A(); B b = new B(); a.aival = 1; b.bival = 2; writefln(a.aival); writefln(b.bival); a.foo(b); b.foo(a); writefln(b.bival); writefln(a.aival); }
 "Kevin VR" <azra pandora.be> schreef in bericht
 news:d31ndi$amo$1 digitaldaemon.com...
 Hello,

 I'm having a problem using templates and the protection attributes for
 class members.
 I can not access private declared methods from an other template class
 defined in the same module.

 For example when I put this in the same module:
 <code>
 public class Foo(T) {
 public this() {
 // Works ok.
 Bar!(T) bar = new Bar!(T)();

 // Fails!? it's private, but in the same module!!
 bar.doSomething();
 }
 }

 public class Bar(T) {
 public this() {
 }

 private void doSomething() {
 }
 }
 </code>

 The D-spec says the following:
 <quote>
 Private means that only members of the enclosing class can access the
 member, or members and functions in the same module as the enclosing
 class.
 </quote>

 I'm sure it has something to do with the templates, because the same  
 thing
 without the templates works fine...
 Does anyone know how to solve this?
Nope. It looks like a bug to me, here is another case demonstrating one that works, and one that doesn't. class Bar(Type) { this() { } private void doSomething() { } } class Baz { this() { } private void doSomething() { } } void main() { //this is ok Baz a = new Baz(); a.doSomething(); //this fails Bar!(int) b = new Bar!(int)(); b.doSomething(); }
I forgot to say, post this to the digitalmars.D.bugs NG pls :) You can post my example or yours or both, up to you. Regan
Apr 07 2005