Bug 693 – 'this' can't be used as an alias parameter for a mixin

Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
All
OS
All
Creation time
2006-12-16T23:26:00Z
Last change time
2014-02-15T13:21:33Z
Keywords
rejects-valid
Assigned to
bugzilla
Creator
wbaxter

Comments

Comment #0 by wbaxter — 2006-12-16T23:26:32Z
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(); }
Comment #1 by andrei — 2010-11-26T13:33:57Z
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
Comment #2 by simen.kjaras — 2011-04-12T01:57:39Z
This also a problem for non-mixin templates: struct MyCallable { int opCall( int n ) { return n; } auto mapthis( int[] arr ) { return map!this( arr ); } }
Comment #3 by simen.kjaras — 2011-04-12T03:01:10Z
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.
Comment #4 by k.hara.pg — 2011-05-07T06:20:37Z
*** Issue 4799 has been marked as a duplicate of this issue. ***
Comment #5 by bugzilla — 2011-07-01T00:31:09Z
Comment #6 by github-bugzilla — 2013-01-21T00:20:12Z
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