Using templates with interfaces

Andrew Chapman via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 25 04:39:27 PDT 2017


Hi guys, I'm a little confused as to whether D supports 
interfaces with templates.  I can compile OK, but linking reports 
an error like this:

Error 42: Symbol Undefined 
_D12relationaldb10interfaces21RÇëÜDBIÇêÿ35__T7loadRowTS6prefix÷6P¶ZÇêáMFAyaAS3std7variant18Çâ└8VÇåìNVki20ZÇëÅZÇûð

Essentially my desired interface looks like this:

interface RelationalDBInterface {
     public T loadRow(T)(string sql, Variant[] params);
}

An example implementation:

public T loadRow(T)(string sql, Variant[] params)
{
	Prepared prepared = prepare(this.conn, sql);
	prepared.setArgs(params);

	auto row = prepared.queryRow();

	if (row.isNull()) {
	    throw new Exception(this.classID ~ "::loadRow - Query 
returned an empty row");
	}

	T item;
	return row.toStruct!T(item);
}

And I would try to call it like this:

auto user = this.loadRow!User(sql, params);

Is it possible, or do I need to rethink the solution?  The idea 
is to pass around a RelationalDBInterface so I can later switch 
from MySQL to Postgres or SQLite or whatever.


More information about the Digitalmars-d-learn mailing list