www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Doubt about alias this

reply d coder <dlang.coder gmail.com> writes:
--047d7b2e44062a3e8804d02d59af
Content-Type: text/plain; charset=ISO-8859-1

Greetings

Kindly take a look at the code below. I am using alias this with a function
that returns by value. I believe in such cases the alias should not take
affect when an instance of foo is being used as an lvalue. As a result
elements of bar should not have been visible inside the constructor of foo.

Please comment. Is this a bug?

Regards
- Puneet

struct bar (bool B, N...) {
  int n = 0;
}

struct foo (N...) {
  bar!(true, N) _bar = void;

  bar!(true, N) getN() {
    return _bar;
  }

  alias getN this;

  public this(T) (T other)
    if (is(T unused : int)) {
    n = other;
  }
}


void main() {
  import std.stdio;
  foo!8 a = 42;
  writeln(a.n);
}

--047d7b2e44062a3e8804d02d59af
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div>Greetings</div><div><br></div><div>Kindly take a look at the code belo=
w. I am using alias this with a function that returns by value. I believe i=
n such cases the alias should not take affect when an instance of foo is be=
ing used as an lvalue. As a result elements of bar should not have been vis=
ible inside the constructor of foo.</div>

<div><br></div><div>Please comment. Is this a bug?</div><div><br></div><div=
Regards</div><div>- Puneet</div><div><br></div><div>struct bar (bool B, N.=

t foo (N...) {</div> <div>=A0 bar!(true, N) _bar =3D void;</div><div>=A0=A0</div><div>=A0 bar!(t= rue, N) getN() {</div><div>=A0 =A0 return _bar;</div><div>=A0 }</div><div><= br></div><div>=A0 alias getN this;</div><div><br></div><div>=A0 public this= (T) (T other)</div> <div>=A0 =A0 if (is(T unused : int)) {</div><div>=A0 =A0 n =3D other;</div>= <div>=A0 }</div><div>}</div><div><br></div><div><br></div><div>void main() = {</div><div>=A0 import std.stdio;</div><div>=A0 foo!8 a =3D 42;</div><div>= =A0 writeln(a.n);</div> <div>}</div><div><br></div> --047d7b2e44062a3e8804d02d59af--
Dec 06 2012
parent "Phil Lavoie" <maidenphil hotmail.com> writes:
Hi,

I tested the code you provided and extended it a little and here 
is the result:

struct bar (bool B, N...) {
  int n = 0;
}

struct foo (N...) {
  bar!(true, N) _bar = void;

  bar!(true, N) getN() {
    return _bar;
  }

  alias getN this;

  public this(T) (T other)
    if (is(T unused : int)) {
    n = other;
  }
}

void aFunc( bar!( true, 8 ) zeBar ) {
   //Subtyping compiles.
}

bar!( true, 8 ) aSecondFunc() {
   return bar!( true, 8 )();
}

struct toto{
   size_t payload;
}

toto aThirdFunc() {
   return toto();
}

void main() {
  import std.stdio;
  foo!8 a = 42;
  writeln(a.n); //Prints 0, as it should.
  aFunc( a ); //The alias this correctly allows for subtyping, as 
advertised
  a.getN().n = 3; //Allowing for rvalue manipulation is indeed 
weird, but apparently in a coherent fashion, not just for the 
alias this case.
  writeln(a.n); //Still prints 0, as it should.
  aSecondFunc().n = 4;
  aThirdFunc().payload = 10; //Without alias this, without 
templates, juste plain rvalue. Another coherent example, yet 
questionably permitted.
}

Therefore, your question, I believe, is more related to rvalues 
directly rather than alias this, if I get your meaning right. The 
question is still very relevant and I was surprised to find out 
about the current behaviors of rvalues.
Dec 06 2012