On Sat, 18 May 2013 01:13:00 +0200, Timothee Cour <thelastmamm...@gmail.com> wrote:

How to have alias this with an unaccessible member (x below).
Making the member private won't work as it'll disable all operations on
said member.

----
struct A(T){
  T x;
  //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++;
  static assert(!__traits(compiles,a.x)); // I want this to hold

}
----


The common way to do it is with a read-only property:

struct A(T) {
  private T x;

  @property
  T get() {
    return x;
  }

  alias get this;
}

void main(){
  auto a = A!int;
  a++; //should do a.x++;
  static assert(!__traits(compiles, a.x)); // This now holds!
static assert(__traits(compiles, a.get)); // As does this, which may or may not be palatable. :(
}

This has been discussed numerous times before, but I believe the current behavior is here to stay.

--
Simen

Reply via email to