Thread overview
[Issue 693] New: 'this' can't be used as an alias parameter for a mixin
Dec 17, 2006
d-bugmail
Apr 12, 2011
Simen Kjaeraas
Apr 12, 2011
Simen Kjaeraas
May 07, 2011
Kenji Hara
Jul 01, 2011
Walter Bright
December 17, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=693

           Summary: 'this' can't be used as an alias parameter for a mixin
           Product: D
           Version: 0.177
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: minor
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: wbaxter@gmail.com


This may not be a bug for some technical reason unbeknownst to me that the spec
mentions (if so downgrade it to enhancement please), but I found it unexpected.
 You can't use 'this' as an alias paramter to a mixin in a class member
function.  The workaround is pretty easy, you just assign it to a local dummy
variable, and use that instead.


import std.stdio : writefln;

template printer_mix(alias T)
{
    void print() {
        writefln(T);
    }
}

class Foo
{
    void dump() {
        // Error: mixin printer_mix!(this) does not match any template
declaration
        mixin printer_mix!(this);

        // this version ok:
        //Foo x = this;
        //mixin printer_mix!(x);

        print();
    }
    char[] toString() { return "I'm Batman"; }
}

void main()
{
    Foo f = new Foo();
    f.dump();
}


-- 

November 26, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=693


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |andrei@metalanguage.com
         AssignedTo|nobody@puremagic.com        |bugzilla@digitalmars.com


--- Comment #1 from Andrei Alexandrescu <andrei@metalanguage.com> 2010-11-26 13:33:57 PST ---
Bug present in 1.065 and 2.050 with different error messages. 1.065:

test.d(14): Error: mixin printer_mix!(this) does not match template declaration
printer_mix(alias T)

2.050:

test.d(14): Error: expression this is not a valid template value argument

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 12, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=693


Simen Kjaeraas <simen.kjaras@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |simen.kjaras@gmail.com


--- Comment #2 from Simen Kjaeraas <simen.kjaras@gmail.com> 2011-04-12 01:57:39 PDT ---
This also a problem for non-mixin templates:

struct MyCallable {
    int opCall( int n ) {
        return n;
    }

    auto mapthis( int[] arr ) {
        return map!this( arr );
    }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 12, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=693



--- Comment #3 from Simen Kjaeraas <simen.kjaras@gmail.com> 2011-04-12 03:01:10 PDT ---
Thinking about this some more, fixing this bug could lead to safer array indexing for one, through a limited system of dependent types:

struct SafeArray( T ) {
    T[] data;

    struct Index( alias arr ) {
        size_t idx;
        // Add safety checks, overflow handling, etc.
    }

    Index!this makeIndex( size_t n ) {
        typeof( return ) result;
        result.idx = n;
        return result;
    }

    T opIndex( Index!this idx ) {
        return data[idx.idx];
    }
}

unittest {
    SafeArray!int arr;
    arr.data = [1,2,3,4,5,6,7,8,9,0];
    auto idx = arr.makeIndex( 3 );
    writeln( arr[idx] ); // Completely safe indexing of array, enforced by type
system.
}

There may be a problem in that compiler considers 'this' a local parameter to a non-global template, which is currently illegal. Not sure how much of a problem this might be in practice.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 07, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=693


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |zan77137@nifty.com


--- Comment #4 from Kenji Hara <k.hara.pg@gmail.com> 2011-05-07 06:20:37 PDT ---
*** Issue 4799 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 01, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=693


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED


--- Comment #5 from Walter Bright <bugzilla@digitalmars.com> 2011-07-01 00:31:09 PDT ---
https://github.com/D-Programming-Language/dmd/commit/5a1f396e915e083ce30c5f09f4e1ef1a9e704fda

https://github.com/D-Programming-Language/dmd/commit/c9e26681a834beb6b211f41234163707453f4ff1

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 21, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=693



--- Comment #6 from github-bugzilla@puremagic.com 2013-01-21 00:20:12 PST ---
Commits pushed to master at https://github.com/D-Programming-Language/d-programming-language.org

https://github.com/D-Programming-Language/d-programming-language.org/commit/9e84dccf6286dd7e718f247bf1a623cd50f68553 fix Issue 693 - 'this' can't be used as an alias parameter for a mixin

https://github.com/D-Programming-Language/d-programming-language.org/commit/0cf09c6b06c13345024772864f6654002904244c Merge pull request #239 from 9rnsr/fix693

Issue 693 - 'this' can't be used as an alias parameter for a mixin

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------