extern(C++, ns)

Rainer Schuetze via Digitalmars-d digitalmars-d at puremagic.com
Mon Jan 11 00:45:51 PST 2016



On 08.01.2016 14:50, Walter Bright wrote:
> On 1/8/2016 12:11 AM, Jacob Carlborg wrote:
>> Walter, should "ns.a()" work in the above example?
>
> No:
>
> 1. first "ns.a" looks up "ns". Finds it in the current module, "main.ns".
> 2. Looks "a" up in "main.ns". "a" is not there. Error.
>
> "a()" works because:
>
> 1. Look up "a" in current module. Doesn't find it.
> 2. Look up "a" in import "foo".
>     1. doesn't find it. Look in imported scope "foo.ns". finds it.
> 3. Look up "a" in import "bar".
>     1. doesn't find it. Look in imported scope "bar.ns". Doesn't find it.
>
> Found only one "a" in the imports. We have da winnah!
>
> I think name lookup rules are straightforward in D. I explain them over
> and over, for years, and nobody but me understands them. I find it very
> frustrating.

Just to clarify: namespaces don't apply "normal" symbol lookup rules (as 
inside structs), but create a "named scope" according to the docs (a 
term never used elsewhere). That means it behaves like a template mixin 
with lookup rules very similar to imports.

Walter, maybe you can show how STL is supposed to be wrapped and used. I 
tried this (using stl as package to avoid conflicts with std):

//////////////////////////////
module stl.map;

extern(C++,std)
struct map(K,V)
{
	V[K] data;
}

//////////////////////////////
module stl.vector;

extern(C++,std)
struct vector(T)
{
	T[] data;
}

//////////////////////////////
import stl.vector;
import stl.map;

//import std.stdio;
//import std.algorithm;

void main()
{
	std.vector!int v;
	std.map!(int,int) m;
	
//	writeln(v);
}

This yields:

test.d(11): Error: stl.vector.std at stl\vector.d(3) conflicts with 
stl.map.std at stl\map.d(3)
test.d(12): Error: stl.vector.std at stl\vector.d(3) conflicts with 
stl.map.std at stl\map.d(3)
test.d(12): Error: template identifier 'map' is not a member of 
namespace 'stl.vector.std'

It gets worse if you add D's std package into the mix by adding the 
commented lines:

test.d(11): Error: template identifier 'vector' is not a member of 
import 'test.std'
test.d(12): Error: template identifier 'map' is not a member of import 
'test.std'

This works to disambiguate, but it is not nice (but maybe inevitable):

void main()
{
	stl.vector.vector!int v;
	stl.map.map!(int,int) m;
	
	writeln(v);
}


More information about the Digitalmars-d mailing list