Trying to use a template class with ranges

mark mark at qtrac.eu
Thu Feb 6 12:16:41 UTC 2020


I am starting on porting Python's difflib's sequence matcher to D.

I want to have a class that will accept two ranges whose elements 
are of the same type and whose elements can be compared for 
equality.

How do I make a class declaration that specifies a (forward) 
range type and an equality-supporting element type?

Here's what doesn't work:

class Diff(T, E) {
	T a; // T should be a forward range of E elements
	T b; // E elements must support == and !=
         // This is a hash key=E element, value=slice of size_t
	size_t[][E] b2j;

	this(T a, T b) {
		this.a = a;
		this.b = b;
		chainB();
	}

	void chainB() {
		foreach (i, element; b)
			b2j[element] ~= i;
		// TODO
	}
}

unittest {
	import std.stdio: writeln;

     writeln("unittest for the diffrange library.");
	auto a = ["Tulips are yellow,", "Violets are blue,",
                   "Agar is sweet,", "As are you."];
	auto b = ["Roses are red,", "Violets are blue,",
                   "Sugar is sweet,", "And so are you."];
	auto diff = Diff(a, b);
}



More information about the Digitalmars-d-learn mailing list