Mixin namespace ambiguity?

Marek Janukowicz marek at janukowicz.net
Sun Sep 15 11:33:43 PDT 2013


The code to reproduce the problem consists of 3 modules:

mix.d:

module mix;

mixin template A( alias x) {

  string a () {
    return x;
  }
}

====================
aux.d:

module aux;

import mix;

mixin A!("a in aux") X;
string b () { return "b in aux"; }

====================
main.d:

module main;

import aux;
import mix;
import std.stdio;
    
mixin A!("a in main") X;
string b () { return "b in main"; }

void main () {
  writefln( "a: %s", X.a );  // Line 1
  //writefln( "a: %s", a );  // Line 2
  writefln( "b: %s", b ); // Line 3
}

I run it with: dmd -run main.d aux.d mix.d

Line 1 works. Line 3 works. Line 2 fails with:
main.d(13): Error: main.A!("a in main").a at mix.d(5) conflicts with aux.A!
("a in aux").a at mix.d(5)

If I omit mixin identifier ("X"), there is no way I can make the call to "a" 
work without prepending module name.

My question is: why calling a function with the same name (from different 
modules) works when:
- it is just a regular function
- it is a mixed-in function with mixin identifier (even though the 
identifier is ambiguous)

and it doesn't when it's a mixed-in function with no mixin identifier.

My first impression is that either both line 1 and 2 should work or neither 
of them should work. It's no surprise to me that line 3 works (and it 
matches the documentation), so I basically included that just for reference.

-- 
Marek Janukowicz


More information about the Digitalmars-d-learn mailing list