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...

Reply via email to