digitalmars.D.learn - private with alias this
- Zhenya (20/20) Dec 24 2012 Hi!
- r_m_r (4/5) Dec 24 2012 check this: http://dpaste.dzfl.pl/eec6d152
- bearophile (4/5) Dec 24 2012 D "private" is enforced only across distinct modules.
- r_m_r (38/39) Dec 24 2012 I've justed tested this and I think you're right (it seems a bit
- r_m_r (8/9) Dec 24 2012 sorry, i made a typo: it should be bar_ instead of _bar.
- evilrat (4/14) Dec 24 2012 i'm not D expert, but isn't the first statement(b.bar_) evaluated
- r_m_r (8/10) Dec 24 2012 i dunno. i'm no D expert either ;-)
- evilrat (6/14) Dec 24 2012 if it's in the same module than it's valid, code in one module
- Zhenya (3/13) Dec 24 2012 So,should I file a new bug?Or it's alright and I don't understand
Hi! Is it correct,that this code doesn't compile: module foo; struct Foo { private int bar; alias bar this; } /////////////////////////////// module main; import foo; void main() { Foo b; int a = b;//Error:undefined identifier 'bar', did you mean 'template back(T) //if (!isNarrowString!(T[]))'? b = a;//cannot implicitly convert expression (a) of type int to Foo } ?
Dec 24 2012
On 12/25/2012 02:06 AM, Zhenya wrote:Is it correct,that this code doesn't compile:check this: http://dpaste.dzfl.pl/eec6d152 regards, r_m_r
Dec 24 2012
r_m_r:check this: http://dpaste.dzfl.pl/eec6d152D "private" is enforced only across distinct modules. Bye, bearophile
Dec 24 2012
On 12/25/2012 06:09 AM, bearophile wrote:D "private" is enforced only across distinct modules.I've justed tested this and I think you're right (it seems a bit counter-intuitive though ;-) ). How about this: //file: foo.d module foo; struct Foo { private int bar_; property int bar() const { return bar_; } property void bar(int b) { bar_ = b; } alias bar this; } //file:main.d module main; import foo; void main() { Foo b = Foo(10); int a = b; assert(a==10); a = 20; b = a; assert(b==20); assert(!__traits(compiles, b._bar)); } //Compiled like so (to avoid linker errors): $ dmd -lib foo.d -oflibfoo.a $ dmd main.d -L-L. -L-lfoo $ ./main regards, r_m_r
Dec 24 2012
On 12/25/2012 07:42 AM, r_m_r wrote:assert(!__traits(compiles, b._bar));sorry, i made a typo: it should be bar_ instead of _bar. Interestingly, the below assertion fails at run time: assert(!__traits(compiles, b.bar_)); but this won't compile (as expected): assert(b.bar_ == 20); //main.d(15): Error: undefined identifier 'bar_' regards, r_m_r
Dec 24 2012
On Tuesday, 25 December 2012 at 02:43:52 UTC, r_m_r wrote:On 12/25/2012 07:42 AM, r_m_r wrote:i'm not D expert, but isn't the first statement(b.bar_) evaluated to b.opCall(Foo.opCall)? so the first assert may be true in this caseassert(!__traits(compiles, b._bar));sorry, i made a typo: it should be bar_ instead of _bar. Interestingly, the below assertion fails at run time: assert(!__traits(compiles, b.bar_)); but this won't compile (as expected): assert(b.bar_ == 20); //main.d(15): Error: undefined identifier 'bar_' regards, r_m_r
Dec 24 2012
On 12/25/2012 10:53 AM, evilrat wrote:i'm not D expert, but isn't the first statement(b.bar_) evaluated to b.opCall(Foo.opCall)? so the first assert may be true in this casei dunno. i'm no D expert either ;-) BTW its interesting that while the following assertion fails at runtime: assert(!__traits(compiles, b.bar_)); the assertion below executes without errors (as expected): assert(!__traits(compiles, b.bar_ == 20)); regards, r_m_r
Dec 24 2012
On Tuesday, 25 December 2012 at 06:19:02 UTC, r_m_r wrote:i dunno. i'm no D expert either ;-) BTW its interesting that while the following assertion fails at runtime: assert(!__traits(compiles, b.bar_)); the assertion below executes without errors (as expected): assert(!__traits(compiles, b.bar_ == 20)); regards, r_m_rif it's in the same module than it's valid, code in one module can access private stuff within the module, so i think TS may put "private alias bar this" and call it from another module, in this case it's behaves correctly as the definition hidden by private modifier.
Dec 24 2012
On Tuesday, 25 December 2012 at 02:43:52 UTC, r_m_r wrote:On 12/25/2012 07:42 AM, r_m_r wrote:So,should I file a new bug?Or it's alright and I don't understand something?assert(!__traits(compiles, b._bar));sorry, i made a typo: it should be bar_ instead of _bar. Interestingly, the below assertion fails at run time: assert(!__traits(compiles, b.bar_)); but this won't compile (as expected): assert(b.bar_ == 20); //main.d(15): Error: undefined identifier 'bar_' regards, r_m_r
Dec 24 2012
On Tuesday, 25 December 2012 at 05:28:54 UTC, Zhenya wrote:On Tuesday, 25 December 2012 at 02:43:52 UTC, r_m_r wrote:i don't think it's a bug, because you are just setting alias for a member. if you remove private from field it would work. i'm just wonder why do you want do something like this?On 12/25/2012 07:42 AM, r_m_r wrote:So,should I file a new bug?Or it's alright and I don't understand something?assert(!__traits(compiles, b._bar));sorry, i made a typo: it should be bar_ instead of _bar. Interestingly, the below assertion fails at run time: assert(!__traits(compiles, b.bar_)); but this won't compile (as expected): assert(b.bar_ == 20); //main.d(15): Error: undefined identifier 'bar_' regards, r_m_r
Dec 24 2012
On Tuesday, 25 December 2012 at 07:21:12 UTC, evilrat wrote:On Tuesday, 25 December 2012 at 05:28:54 UTC, Zhenya wrote:After your explaination I understood that it's really meaningless.Thank you)On Tuesday, 25 December 2012 at 02:43:52 UTC, r_m_r wrote:i don't think it's a bug, because you are just setting alias for a member. if you remove private from field it would work. i'm just wonder why do you want do something like this?On 12/25/2012 07:42 AM, r_m_r wrote:So,should I file a new bug?Or it's alright and I don't understand something?assert(!__traits(compiles, b._bar));sorry, i made a typo: it should be bar_ instead of _bar. Interestingly, the below assertion fails at run time: assert(!__traits(compiles, b.bar_)); but this won't compile (as expected): assert(b.bar_ == 20); //main.d(15): Error: undefined identifier 'bar_' regards, r_m_r
Dec 25 2012