www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What's wrong with this alias?

reply Dr.No <jckj33 gmail.com> writes:
consider this:

module report;

// output an error message on stderr
void error(A...)(string fmt, A args)
{
	import colorize : fg, color, cwriteln, cwritefln, cwrite;
	stderr.cwrite("error: ".color(fg.yellow));
	cwritefln(fmt.color(fg.yellow), args);
}

void warning(A...)(string fmt, A args)
{
	import colorize : fg, color, cwriteln, cwritefln, cwrite;
	cwrite("warning: ".color(fg.blue));
	cwritefln(fmt.color(fg.blue), args);
}

then

class C
{

	void error(A...)(string fmt, A args)
	{
		import report : error;
		reportedAnyError = true;
		error(fmt, args);
	}
	alias warning = report.warning;
}


I got this:

Error: undefined identifier report.warning

but this works:

	void warning(A...)(string fmt, A args)
	{
		import report : warning;
		warning(fmt, args);
	}

why alias cannot find my symbol there?
Apr 26 2018
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 04/26/2018 10:56 AM, Dr.No wrote:

 class C
 {

      void error(A...)(string fmt, A args)
      {
          import report : error;
          reportedAnyError = true;
          error(fmt, args);
      }
      alias warning = report.warning;
 }


 I got this:

 Error: undefined identifier report.warning
If that really is the code, you're forgetting to import report in that scope. Otherwise, that kind of alias works at least in this simple case: void foo(A...)(A a) { import std.stdio; writeln(a); } alias bar = foo; void main() { bar(42, "hello"); } Ali
Apr 26 2018