how to have alias this with an unaccessible member?

Timothee Cour thelastmammoth at gmail.com
Thu May 23 22:25:13 PDT 2013


This won't do for the same reason: now 'get' is made public so we're back
to the same problem (inverting roles of x and get).

However what about changing the behavior of alias this as follows:

when a member/method x is private, "alias x this" behaves as if x was not
declared private.

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 any
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, we
would have implemented the same concept as "embedding" in GO.

Any thoughts?


here's what we would have:
----
struct A(T){
  private T x would prevent alias this from doing anything useful
  alias x this;
}
void main(){
  auto a=A!int;
  a++;//should do a.x++; //semantic change: even though x is private, all
its methods are un-privated through alias this.
  static assert(!__traits(compiles,a.x));
}
----




On Sat, May 18, 2013 at 1:33 AM, Dicebot <m.strashun at gmail.com> wrote:

> Will this do?
>
> --------------------
>
>
> struct A(T)
> {
>     private T x;
>
>     ref T get()
>
>     {
>         return x;
>     }
>
>     alias get this;
> }
>
> ---------------------
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20130523/ca17b8d7/attachment.html>


More information about the Digitalmars-d-learn mailing list