operator overloading outside the type

Salih Dincer salihdb at hotmail.com
Sun Mar 30 11:28:09 UTC 2025


On Saturday, 29 March 2025 at 16:31:08 UTC, Steven Schveighoffer 
wrote:
> 
> ...This goes for member functions, alias this, etc. Hijacking 
> something inside the type from outside the type should not be 
> allowed.

It's a great perspective...🤓

```d
imoort std.stdio;

struct F {
   //void prnOut() { this.writeln; }/*
   void prnOut() {
     .prnOut(this);
   }//*/
}

struct C {  F f; alias f this; }

void main()
{
   void prnOut(T)(ref T t) { writeln("local"); }
   C c; // no UFCS (deceptive):
     c.prnOut(); // "global"
   prnOut(c);    // "local"
   .prnOut(c);   // "global"
}

void prnOut(T)(ref T t) { writeln("global"); }
```

SDB at 79


More information about the Digitalmars-d mailing list