Make all functions from std.typecons "Proxy" inout

Kenji Hara k.hara.pg at gmail.com
Thu May 10 23:56:30 PDT 2012


On Thursday, 10 May 2012 at 07:32:42 UTC, Namespace wrote:
> Can you explain me how TemplateThisParameter works? I read in 
> the
> manual but a more detail explanation would help me a lot.

Hmm, I have thought following code should work about foo, but 
doesn't.

import std.stdio;
struct Proxy
{
     void foo(this T)()
     {
         writeln(T.stringof);
         static assert(is(typeof(this) == T));
         // I have thought this should pass with all cases...
     }

     void bar(this T)() inout
     {
         writeln(T.stringof);
         //static assert(is(typeof(this) == T));
         static assert(is(typeof(this) == inout(Proxy)));
     }
     void bar(this T)() shared inout
     {
         writeln(T.stringof);
         //static assert(is(typeof(this) == T));
         static assert(is(typeof(this) == shared(inout(Proxy))));
     }
}
void main()
{
     Proxy mp;
     const Proxy cp;
     immutable Proxy ip;
     shared Proxy sp;
     shared const Proxy scp;

     mp.foo();   // expects: Proxy -> OK
/+
     cp.foo();   // expects: const(Proxy)  -> NG!
     ip.foo();   // expects: immutable(Proxy)  -> NG!
     sp.foo();   // expects: shared(Proxy)  -> NG!
     scp.foo();  // expects: shared(const(Proxy))  -> NG!
+/
     mp.bar();   // expects: Proxy -> OK
     cp.bar();   // expects: const(Proxy) -> OK
     ip.bar();   // expects: immutable(Proxy) -> OK
     sp.bar();   // expects: shared(Proxy) -> OK
     scp.bar();  // expects: shared(const(Proxy)) -> OK
}

The spec:
http://dlang.org/template#TemplateThisParameter
doesn't talk about typeof(this).

I think current behavior is less useful than I have thought.
And, current std.typecons.Proxy doesn't work as expected for 
non-mutable objects...


More information about the Digitalmars-d-learn mailing list