digitalmars.D.learn - alias this of non-public member
- =?UTF-8?B?Ik3DoXJjaW8=?= Martins" (33/33) Apr 07 2015 Hi!
- Daniel Kozak via Digitalmars-d-learn (19/61) Apr 07 2015 Works for me:
- Dude (2/65) Apr 07 2015 Ok I see, it does not work if you try it outside of same file.
- Namespace (14/77) Apr 07 2015 Modules are like friends in C++: Even private members can be
- Daniel Kozak (27/90) Apr 07 2015 module some;
- Daniel Kozak (3/100) Apr 07 2015 And maybe Proxy can be use for your use case:
- =?UTF-8?B?Ik3DoXJjaW8=?= Martins" (2/110) Apr 07 2015 Proxy doesn't really help here :(
- Vlad Levenfeld (4/5) Apr 07 2015 Nothing will help you get around this. You have to expose a
Hi! Excuse me if this is obvious, but I can't recall coming across anything similar and a quick search returns nothing relevant: struct Foo { } struct FooWrapper { alias x_ this; private Foo* x_; // doesn't work, as x_ is private } Basically, I want x_ to never be visible, except through the "alias this" mechanism, at which point it should instead be seen as public. Assuming something like this is not already possible in a clean way, I would like to suggest a tiny(I think) addition to the language: struct FooWrapper { public alias x_ this; // overrides the visibility through the alias; private Foo* x_; } While I think this would be useful for the language, the reason I want such a wrapper, is because I want to give opIndex, toString, to a pointer, or, in fact just value semantics, while keeping the rest of the interface through the pointer. I thought about using a class instead of a struct pointer, but I am not sure about the memory layout for classes, nor about the efficiency of overriding Object's methods, so I didn't want to risk making it any less efficient. If someone could shed some light about D's class memory layout and general performance differences to a simple struct (or a C++ class for that matter), that would also be great. In general, more information about these sort of things would be great for us also-C++ programmers. :)
Apr 07 2015
On Tue, 07 Apr 2015 16:40:29 +0000 via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Hi! Excuse me if this is obvious, but I can't recall coming across anything similar and a quick search returns nothing relevant: struct Foo { } struct FooWrapper { alias x_ this; private Foo* x_; // doesn't work, as x_ is private } Basically, I want x_ to never be visible, except through the "alias this" mechanism, at which point it should instead be seen as public. Assuming something like this is not already possible in a clean way, I would like to suggest a tiny(I think) addition to the language: struct FooWrapper { public alias x_ this; // overrides the visibility through the alias; private Foo* x_; } While I think this would be useful for the language, the reason I want such a wrapper, is because I want to give opIndex, toString, to a pointer, or, in fact just value semantics, while keeping the rest of the interface through the pointer. I thought about using a class instead of a struct pointer, but I am not sure about the memory layout for classes, nor about the efficiency of overriding Object's methods, so I didn't want to risk making it any less efficient. If someone could shed some light about D's class memory layout and general performance differences to a simple struct (or a C++ class for that matter), that would also be great. In general, more information about these sort of things would be great for us also-C++ programmers. :)Works for me: struct M { void callMe() { writeln("Ring..."); } } struct S { alias m this; private M m; } void main(string[] args) { S s; s.callMe(); }
Apr 07 2015
On Tuesday, 7 April 2015 at 17:21:09 UTC, Daniel Kozak wrote:On Tue, 07 Apr 2015 16:40:29 +0000 via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Ok I see, it does not work if you try it outside of same file.Hi! Excuse me if this is obvious, but I can't recall coming across anything similar and a quick search returns nothing relevant: struct Foo { } struct FooWrapper { alias x_ this; private Foo* x_; // doesn't work, as x_ is private } Basically, I want x_ to never be visible, except through the "alias this" mechanism, at which point it should instead be seen as public. Assuming something like this is not already possible in a clean way, I would like to suggest a tiny(I think) addition to the language: struct FooWrapper { public alias x_ this; // overrides the visibility through the alias; private Foo* x_; } While I think this would be useful for the language, the reason I want such a wrapper, is because I want to give opIndex, toString, to a pointer, or, in fact just value semantics, while keeping the rest of the interface through the pointer. I thought about using a class instead of a struct pointer, but I am not sure about the memory layout for classes, nor about the efficiency of overriding Object's methods, so I didn't want to risk making it any less efficient. If someone could shed some light about D's class memory layout and general performance differences to a simple struct (or a C++ class for that matter), that would also be great. In general, more information about these sort of things would be great for us also-C++ programmers. :)Works for me: struct M { void callMe() { writeln("Ring..."); } } struct S { alias m this; private M m; } void main(string[] args) { S s; s.callMe(); }
Apr 07 2015
On Tuesday, 7 April 2015 at 17:21:09 UTC, Daniel Kozak wrote:On Tue, 07 Apr 2015 16:40:29 +0000 via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Modules are like friends in C++: Even private members can be accessed. Márcio Martins: You need a public getter function: ---- property nogc safe inout(Foo*) getFoo() inout pure nothrow { return x_; } alias getFoo this; ----Hi! Excuse me if this is obvious, but I can't recall coming across anything similar and a quick search returns nothing relevant: struct Foo { } struct FooWrapper { alias x_ this; private Foo* x_; // doesn't work, as x_ is private } Basically, I want x_ to never be visible, except through the "alias this" mechanism, at which point it should instead be seen as public. Assuming something like this is not already possible in a clean way, I would like to suggest a tiny(I think) addition to the language: struct FooWrapper { public alias x_ this; // overrides the visibility through the alias; private Foo* x_; } While I think this would be useful for the language, the reason I want such a wrapper, is because I want to give opIndex, toString, to a pointer, or, in fact just value semantics, while keeping the rest of the interface through the pointer. I thought about using a class instead of a struct pointer, but I am not sure about the memory layout for classes, nor about the efficiency of overriding Object's methods, so I didn't want to risk making it any less efficient. If someone could shed some light about D's class memory layout and general performance differences to a simple struct (or a C++ class for that matter), that would also be great. In general, more information about these sort of things would be great for us also-C++ programmers. :)Works for me: struct M { void callMe() { writeln("Ring..."); } } struct S { alias m this; private M m; } void main(string[] args) { S s; s.callMe(); }
Apr 07 2015
On Tuesday, 7 April 2015 at 17:21:09 UTC, Daniel Kozak wrote:On Tue, 07 Apr 2015 16:40:29 +0000 via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:module some; import std.stdio; Another way is use template mixin: private mixin template M() { int someVar = 7; public void callMe() { writeln("Call"); } public void callMe2() { writeln("Call2"); } } struct S { mixin M; } //// module main; import some; void main(string[] args) { S s; s.callMe(); s.callMe2(); }Hi! Excuse me if this is obvious, but I can't recall coming across anything similar and a quick search returns nothing relevant: struct Foo { } struct FooWrapper { alias x_ this; private Foo* x_; // doesn't work, as x_ is private } Basically, I want x_ to never be visible, except through the "alias this" mechanism, at which point it should instead be seen as public. Assuming something like this is not already possible in a clean way, I would like to suggest a tiny(I think) addition to the language: struct FooWrapper { public alias x_ this; // overrides the visibility through the alias; private Foo* x_; } While I think this would be useful for the language, the reason I want such a wrapper, is because I want to give opIndex, toString, to a pointer, or, in fact just value semantics, while keeping the rest of the interface through the pointer. I thought about using a class instead of a struct pointer, but I am not sure about the memory layout for classes, nor about the efficiency of overriding Object's methods, so I didn't want to risk making it any less efficient. If someone could shed some light about D's class memory layout and general performance differences to a simple struct (or a C++ class for that matter), that would also be great. In general, more information about these sort of things would be great for us also-C++ programmers. :)Works for me: struct M { void callMe() { writeln("Ring..."); } } struct S { alias m this; private M m; } void main(string[] args) { S s; s.callMe(); }
Apr 07 2015
On Tuesday, 7 April 2015 at 17:43:08 UTC, Daniel Kozak wrote:On Tuesday, 7 April 2015 at 17:21:09 UTC, Daniel Kozak wrote:And maybe Proxy can be use for your use case:On Tue, 07 Apr 2015 16:40:29 +0000 via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:module some; import std.stdio; Another way is use template mixin: private mixin template M() { int someVar = 7; public void callMe() { writeln("Call"); } public void callMe2() { writeln("Call2"); } } struct S { mixin M; } //// module main; import some; void main(string[] args) { S s; s.callMe(); s.callMe2(); }Hi! Excuse me if this is obvious, but I can't recall coming across anything similar and a quick search returns nothing relevant: struct Foo { } struct FooWrapper { alias x_ this; private Foo* x_; // doesn't work, as x_ is private } Basically, I want x_ to never be visible, except through the "alias this" mechanism, at which point it should instead be seen as public. Assuming something like this is not already possible in a clean way, I would like to suggest a tiny(I think) addition to the language: struct FooWrapper { public alias x_ this; // overrides the visibility through the alias; private Foo* x_; } While I think this would be useful for the language, the reason I want such a wrapper, is because I want to give opIndex, toString, to a pointer, or, in fact just value semantics, while keeping the rest of the interface through the pointer. I thought about using a class instead of a struct pointer, but I am not sure about the memory layout for classes, nor about the efficiency of overriding Object's methods, so I didn't want to risk making it any less efficient. If someone could shed some light about D's class memory layout and general performance differences to a simple struct (or a C++ class for that matter), that would also be great. In general, more information about these sort of things would be great for us also-C++ programmers. :)Works for me: struct M { void callMe() { writeln("Ring..."); } } struct S { alias m this; private M m; } void main(string[] args) { S s; s.callMe(); }
Apr 07 2015
On Tuesday, 7 April 2015 at 17:59:57 UTC, Daniel Kozak wrote:On Tuesday, 7 April 2015 at 17:43:08 UTC, Daniel Kozak wrote:Proxy doesn't really help here :(On Tuesday, 7 April 2015 at 17:21:09 UTC, Daniel Kozak wrote:And maybe Proxy can be use for your use case:On Tue, 07 Apr 2015 16:40:29 +0000 via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:module some; import std.stdio; Another way is use template mixin: private mixin template M() { int someVar = 7; public void callMe() { writeln("Call"); } public void callMe2() { writeln("Call2"); } } struct S { mixin M; } //// module main; import some; void main(string[] args) { S s; s.callMe(); s.callMe2(); }Hi! Excuse me if this is obvious, but I can't recall coming across anything similar and a quick search returns nothing relevant: struct Foo { } struct FooWrapper { alias x_ this; private Foo* x_; // doesn't work, as x_ is private } Basically, I want x_ to never be visible, except through the "alias this" mechanism, at which point it should instead be seen as public. Assuming something like this is not already possible in a clean way, I would like to suggest a tiny(I think) addition to the language: struct FooWrapper { public alias x_ this; // overrides the visibility through the alias; private Foo* x_; } While I think this would be useful for the language, the reason I want such a wrapper, is because I want to give opIndex, toString, to a pointer, or, in fact just value semantics, while keeping the rest of the interface through the pointer. I thought about using a class instead of a struct pointer, but I am not sure about the memory layout for classes, nor about the efficiency of overriding Object's methods, so I didn't want to risk making it any less efficient. If someone could shed some light about D's class memory layout and general performance differences to a simple struct (or a C++ class for that matter), that would also be great. In general, more information about these sort of things would be great for us also-C++ programmers. :)Works for me: struct M { void callMe() { writeln("Ring..."); } } struct S { alias m this; private M m; } void main(string[] args) { S s; s.callMe(); }
Apr 07 2015
On Tuesday, 7 April 2015 at 19:17:41 UTC, Márcio Martins wrote:>Proxy doesn't really help here :(Nothing will help you get around this. You have to expose a public member and alias to that. Try wrapping the access in a public zero-parameter member function.
Apr 07 2015