Jump to page: 1 2
Thread overview
private with alias this
Dec 24, 2012
Zhenya
Dec 25, 2012
r_m_r
Dec 25, 2012
bearophile
Dec 25, 2012
r_m_r
Dec 25, 2012
r_m_r
Dec 25, 2012
evilrat
Dec 25, 2012
r_m_r
Dec 25, 2012
evilrat
Dec 25, 2012
Zhenya
Dec 25, 2012
evilrat
Dec 25, 2012
Zhenya
December 24, 2012
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
}
?
December 25, 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
December 25, 2012
r_m_r:

> check this: http://dpaste.dzfl.pl/eec6d152

D "private" is enforced only across distinct modules.

Bye,
bearophile
December 25, 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
December 25, 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

December 25, 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:
>> 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

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 case
December 25, 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:
>> 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

So,should I file a new bug?Or it's alright and I don't understand something?
December 25, 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 case

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_r
December 25, 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_r

if 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.
December 25, 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:
>> 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
>
> So,should I file a new bug?Or it's alright and I don't understand something?

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?
« First   ‹ Prev
1 2