Algorithms should be free from rich types

Steven Schveighoffer schveiguy at gmail.com
Fri Jun 30 02:15:21 UTC 2023


On 6/28/23 4:00 AM, FeepingCreature wrote:

> I like this approach:
> 
> ```
> class C {
>      private int i;
> }
> ...
> void main() @system {
>      auto c = new C;
>      c.private.i = 5;
> }
> ```
> 

```d
auto usePrivate(T)(ref T thing) @system
{
    static struct GetMeThePrivateStuff
    {
      @disable this(this); // shouldn't be copied about, meant to be a 
temporary access
      private T* _thing; // "private" lol
      auto ref opDispatch(string s, Args...)(Args args)
      {
         static if(Args.length == 0)
            return __traits(getMember, *_thing, s);
         else
            return __traits(getMember, *_thing, s)(args);
      }
    }

    return GetMeThePrivateStuff(&thing);
}
```

Yeah, yeah, it needs work. But you get the idea. D is all-powerful.

-Steve


More information about the Digitalmars-d mailing list