www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - import in mixin template

reply vitus <vitus vitus.vitus> writes:
Code:

mixin template MIXIN(){
	import std.stdio;

         alias m = writeln;  //OK
}

class CLASS{

	mixin MIXIN;

	alias wln1 = writeln;			//Error: 'writeln' is not defined, 
perhaps you need to import std.stdio; ?
	alias wln2 = std.stdio.writeln;	//OK
	void test(){
		wln2("test");				//OK
		std.stdio.writeln("test");	//Error: Deprecation: module 
std.stdio is not accessible here, perhaps add 'static import 
std.stdio;'
	}
}

Why 'alias wln1 = writeln;' doesnt't work but 'alias wln2 = 
std.stdio.writeln;' work when import is not static?
Why 'wln2("test");' work but 'std.stdio.writeln("test");' 
doesn't? (changing import to static import doesn't change 
anything)
May 25 2016
parent reply Era Scarecrow <rtcvb32 yahoo.com> writes:
On Thursday, 26 May 2016 at 05:20:25 UTC, vitus wrote:
 Why 'alias wln1 = writeln;' doesnt't work but 'alias wln2 = 
 std.stdio.writeln;' work when import is not static? Why 
 'wln2("test");' work but 'std.stdio.writeln("test");' doesn't? 
 (changing import to static import doesn't change anything)
The import seems to be localized to just the mixin & the template. Makes sense since you don't want to be cluttering the namespace with other imports. The only thing you haven't shown is that the 'alias m' is identified and accessible. Adding it in the compiler doesn't complain; Only line 11 (alias wln1) complains for me DMD w32 v2.071.0.
May 25 2016
parent vitus <vitus vitus.vitus> writes:
On Thursday, 26 May 2016 at 05:47:36 UTC, Era Scarecrow wrote:
 On Thursday, 26 May 2016 at 05:20:25 UTC, vitus wrote:
 Why 'alias wln1 = writeln;' doesnt't work but 'alias wln2 = 
 std.stdio.writeln;' work when import is not static? Why 
 'wln2("test");' work but 'std.stdio.writeln("test");' doesn't? 
 (changing import to static import doesn't change anything)
The import seems to be localized to just the mixin & the template. Makes sense since you don't want to be cluttering the namespace with other imports. The only thing you haven't shown is that the 'alias m' is identified and accessible. Adding it in the compiler doesn't complain; Only line 11 (alias wln1) complains for me DMD w32 v2.071.0.
But why is 'std.stdio.writeln' accessible from CLASS but writeln no? Both are imported in MIXIN. and: mixin template MIXIN(){ import std.stdio; } class CLASS{ mixin MIXIN; void test(){ alias wln = std.stdio.writeln; //OK wln("test"); //OK std.stdio.writeln("test"); //Error: Deprecation: module std.stdio is not accessible here, perhaps add 'static import std.stdio;' } } Why is possible create alias to function 'std.stdio.writeln' but cannot by called as a function directly?
May 25 2016