www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - go embeddings and "public alias x this;" on a private member x

--047d7b471d9c00e35904dd985ecb
Content-Type: text/plain; charset=ISO-8859-1

I'd like public alias x this to reset protection attribute on a (private)
member x:

b.d:
struct B(T){
    private T x;// would normally prevent alias this from doing anything
useful
    public alias x this;
}

a.d:
void main(){
    auto a=B!int();
    a++;//should do a.x++; semantic change: even though x is private,
public alias x this should behave as if x were public.
    //a.x++ should still be compile error of course.
    static assert(!__traits(compiles,a.x)); //fails but seems like a bug; I
just reported it: 10170

}


(likewise with protected alias this, etc).

I think this makes sense:
* this allows protection (x is an implementation detail) so that 'this'
behaves exactly as 'x'
* also, without this change of behavior, "alias x this" would not make much
sense in terms of behavior outside the class (inside behavior should just
access x directly)

Then, when multiple alias this statements will become allowed in D (will
it?), we would have implemented something similar to go "embeddings" in GO
(see http://golang.org/doc/effective_go.html)

(note, I posted an earlier form of this in
http://forum.dlang.org/post/mailman.97.1369373126.13711.digitalmars-d-learn puremagic.com
)

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

<div>I&#39;d like public alias x this to=A0reset protection attribute on a =
(private) member x:</div><div><br></div><div><div>b.d:</div><div>struct B(T=
){</div><div>=A0 =A0 private T x;// would normally prevent alias this from =
doing anything useful</div>
<div>=A0 =A0 public alias x this;=A0</div><div>}</div><div><br></div><div>a=
.d:</div><div>void main(){</div><div>=A0 =A0 auto a=3DB!int();</div><div>=
=A0 =A0 a++;//should do a.x++; semantic change: even though x is private, p=
ublic alias x this should behave as if x were public.</div>
<div>=A0 =A0 //a.x++ should still be compile error of course.</div><div>=A0=
 =A0 static assert(!__traits(compiles,a.x)); //fails but seems like a bug; =
I just reported it: 10170</div><div><br></div><div>}</div></div><div><font =
face=3D"Menlo" style=3D"font-size:small"><font color=3D"#444444"><br>
</font></font></div><div><font face=3D"Menlo"><font color=3D"#444444"><br><=
/font></font></div><div><font face=3D"Menlo"><font color=3D"#444444">(likew=
ise with protected alias this, etc).</font></font></div><div><font face=3D"=
Menlo"><font color=3D"#444444"><br>
</font></font></div><div><div style=3D"color:rgb(34,34,34);font-family:aria=
l,sans-serif;font-size:13px;background-color:rgb(255,255,255)">I think this=
 makes sense:=A0</div><div style=3D"color:rgb(34,34,34);font-family:arial,s=
ans-serif;font-size:13px;background-color:rgb(255,255,255)">
* this allows protection (x is an implementation detail) so that &#39;this&=
#39; behaves exactly as &#39;x&#39;</div><div style=3D"color:rgb(34,34,34);=
font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,25=
5)">
* also, without this change of behavior, &quot;alias x this&quot; would not=
 make much sense in terms of behavior outside the class (inside behavior sh=
ould just access x directly)</div><div style=3D"color:rgb(34,34,34);font-fa=
mily:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<br></div><div style=3D"color:rgb(34,34,34);font-family:arial,sans-serif;fo=
nt-size:13px;background-color:rgb(255,255,255)">Then, when multiple alias t=
his statements will become allowed in D (will it?), we would have implement=
ed something similar to go &quot;embeddings&quot; in GO (see=A0<a href=3D"h=
ttp://golang.org/doc/effective_go.html">http://golang.org/doc/effective_go.=
html</a>)</div>
</div><div style=3D"color:rgb(34,34,34);font-family:arial,sans-serif;font-s=
ize:13px;background-color:rgb(255,255,255)"><br></div><div style=3D"color:r=
gb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:r=
gb(255,255,255)">
(note, I posted an earlier form of this in=A0<a href=3D"http://forum.dlang.=
org/post/mailman.97.1369373126.13711.digitalmars-d-learn puremagic.com">htt=
p://forum.dlang.org/post/mailman.97.1369373126.13711.digitalmars-d-learn pu=
remagic.com</a>)</div>
<div style=3D"color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13=
px;background-color:rgb(255,255,255)"><br></div><div><br></div>

--047d7b471d9c00e35904dd985ecb--
May 25 2013