www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - template instance testA.FormatParser!(F) forward reference of variable

reply "ref2401" <refactor24 gmail.com> writes:
Everything works fine until 'put' method in 'testB' module is 
uncommented. With that code I'm getting error saying 'template 
instance testA.FormatParser!(F) forward reference of variable 
F.'. What does it mean and why is this happening ( to me:)) ]?


module testA; // ------

class MyClass(string Format) {
	alias FormatParser!Format format;

	mixin(injectFields());
	mixin(injectCtor());


	private static string injectCtor() {
		string decl = "this(int arg0";
		if (format.hasSecondSpec) {
			decl ~= ", int arg1";
		}

		decl ~= "){";
		decl ~= format.first ~ " = arg0;";
		if (format.hasSecondSpec) {
			decl ~= format.second ~ " = arg1;";
		}
		decl ~= "}";

		return decl;
	}

	private static string injectFields() {
		string decl = "int " ~ format.first ~ ";";
		if (format.hasSecondSpec) {
			decl ~= "int " ~ format.second ~ ";";
		}

		return decl;
	}
}

template FormatParser(string Format) {
	enum delimiterIndex = findDelimiterIndex(Format);

	static if (delimiterIndex != -1) {
		enum string first = Format[0 .. delimiterIndex];
		enum string second = Format[delimiterIndex + 2 .. $];
		enum bool hasSecondSpec = true;
	} else {
		enum string first = Format;
		enum string second = null;
		enum bool hasSecondSpec = false;
	}

	private int findDelimiterIndex(string fmt) pure {
		for (int i = 0; i < fmt.length - 1; ++i) {
			if (fmt[i] == '|' && fmt[i + 1] == '|') {
				return i;
			}
		}

		return -1;
	}
}


module main; // ------

import testA;
import testB;

import std.stdio;
static import std.string;



int main(string[] argv) {
	auto o = new MyClass!("aaa||bbb")(89, 7);

	writeln(o.aaa, " ", o.bbb);	// prints 89 7
	readln();
	return 0;
}


module testB; // ------

import testA;
import std.stdio;


//void put(T : MyClass!F, string F)(T obj) {
//
//}
Oct 08 2013
parent "ref2401" <refactor24 gmail.com> writes:
i've found the solution:

void put(T)(T mesh) {
	static if (is(T : MyClass!F, string F)) {
		writeln("put method: ", F);
	} else {
		static assert(0, "Invalid type '" ~ 
std.traits.fullyQualifiedName!T ~ "'.");
	}
}
Oct 09 2013