how to have alias this with an unaccessible member?

Timothee Cour thelastmammoth at gmail.com
Fri May 17 17:12:00 PDT 2013


so in what you suggest, the exact same problem remains with 'get' being
exposed instead of 'x', so the situation didn't improve...

looks like it's impossible to achieve this?


On Fri, May 17, 2013 at 4:24 PM, Simen Kjaeraas <simen.kjaras at gmail.com>wrote:

> On Sat, 18 May 2013 01:13:00 +0200, Timothee Cour <
> thelastmammoth at 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20130517/46d1ce27/attachment.html>


More information about the Digitalmars-d-learn mailing list