www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 693] New: 'this' can't be used as an alias parameter for a mixin

reply d-bugmail puremagic.com writes:
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();
}


-- 
Dec 16 2006
next sibling parent d-bugmail puremagic.com writes:
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: -------
Nov 26 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
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: -------
Apr 12 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
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: -------
Apr 12 2011
prev sibling parent d-bugmail puremagic.com writes:
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: -------
May 07 2011