Explicit this for methods

vit vit at vit.vit
Tue Jul 23 14:00:30 UTC 2024


On Tuesday, 23 July 2024 at 11:42:03 UTC, Quirin Schroll wrote:
> ...

This dip idea allow this:

```d
     template C(bool const_)
     {
         class C{
             abstract void test1(This this){/*body 1*/}
             abstract void test2(This this, int i){/*body 2*/}
             abstract void test3(This this, string str){/*body 3*/}
         }

         static if(const_)
         	alias This = const typeof(this);
         else
             alias This = typeof(this);
     }
```
instead of:

```d
     template C(bool const_){

         class C{
             static if(const_){
                 abstract void test1()const{return 
test1_impl(this);}
                 abstract void test2(int i)const{test2_impl(this, 
i);}
                 abstract void test3(string 
str)const{test3_impl(this, str);}
             }
             else{
                 abstract void test1(){return test1_impl(this);}
                 abstract void test2(int i){test2_impl(this, i);}
                 abstract void test3(string str){test3_impl(this, 
str);}
             }
         }

         static if(const_)
         	alias This = const C;
         else
             alias This = C;

         void test1_impl(This self){/*body 1*/}
         void test2_impl(This self, int i){/*body 2*/}
         void test3_impl(This self, string str){/*body 3*/}
     }

```

Other example are copy constructors for ptr wrapper (like rc ptr, 
shared ptr, ...):

```d

     template Ptr(T){
         private void* ptr;

         static foreach(alias Rhs; AliasSeq!(Ptr, const Ptr, 
immutable Ptr))
         static foreach(alias Lhs; AliasSeq!(Ptr, const Ptr, 
immutable Ptr))
         {
             static if(is(CopyTypeQualifiers!(Rhs, T*) : 
CopyTypeQualifiers!(Lhs, T*)))
                 this(Lhs this, ref Rhs rhs)@trusted{
                     this.ptr = cast(typeof(this.ptr))rhs.ptr;
                 }
             else
                 @disable this(Lhs this, ref Rhs rhs)@trusted;
         }

     }
```

instead of:

```d

     struct Ptr(T){
         private void* ptr;

         static foreach(alias Rhs; AliasSeq!(Ptr, const Ptr, 
immutable Ptr))
         {

             static if(is(CopyTypeQualifiers!(Rhs, T*) : T*))
                 this(ref Rhs rhs)@trusted{
                     this.ptr = cast(typeof(this.ptr))rhs.ptr;
                 }
             else
                 @disable this(ref Rhs rhs)@trusted;

             static if(is(CopyTypeQualifiers!(Rhs, T*) : 
const(T*)))
                 this(ref Rhs rhs)@trusted const{
                     this.ptr = cast(typeof(this.ptr))rhs.ptr;
                 }
             else
                 @disable this(ref Rhs rhs)@trusted const;

             static if(is(CopyTypeQualifiers!(Rhs, T*) : 
immutable(T*)))
                 this(ref Rhs rhs)@trusted immutable{
                     this.ptr = cast(typeof(this.ptr))rhs.ptr;
                 }
             else
                 @disable this(ref Rhs rhs)@trusted immutable;
         }

     }
```

It simplify code and make forwarding parameters unnecessary.


>>
>> ```d
>> class C{
>> […]
>> 	abstract void test2(const C this)pure;	//method with explicit 
>> this with type (is(Unqual!T == C))
>> ```
> What is `T`? Did you intend `Unqual!(typeof(this))`?

Yes but `typeof(this)` in this context in unclear too. :) (it is 
type of aggregate type in witch is method, not type of `this` 
parameter)



More information about the dip.ideas mailing list