Differing implementations for a function in two interfaces

Lionello Lunesu lio at lunesu.remove.com
Sat Apr 15 02:33:55 PDT 2006


Hi,

Like the subject says, I have two interfaces that both declare some 
function, say "int get();", but in the class that implements these 
interfaces I'd like to differentiate between the two.

#interface IA {
#	int get();
#}

#interface IB {
#	int get();
#}

#class C : IA, IB {
#	int get() { return 1; }
#}

I would like to implement IA.get() differently from IB.get(), but is 
this even possible? Can't find anything on this in the docs.

Actually, what I'm trying to do is create multiple foreach/opApply's in 
one class, depending on which interface you use, specifically a wrapper 
for the sqlite "sqlite3_stmt" handle. I'd like to be able to iterate the 
rows in a result set, and the fields in a row.

At the moment I do this:

#// handle wrapper, RAII
#class Command {
#	sqlite3_stmt* stmt;
#	~this() { sqlite3_finalize(stmt); }
#	Result Execute() { return new Result(this); }
#}

#class Result {
#	Command cmd;
#	// this one iterates rows; does new ResultRow(cmd)
#	int opApply( int delegate(inout ResultRow) dg );
#}

#class ResultRow {
#	Command cmd;
#	// this one iterates fields; does new ResultField(cmd,fieldno);
#	int opApply( int delegate(inout ResultField) dg );
#}

#class ResultField {
#	Command cmd;
#	uint fieldno;
#}

This works just fine, but a lot of temporary objects are created, that 
don't really contain anything new, just a reference to the Command 
object. I would like to use interfaces instead:

#interface IResult {
#	// this one iterates rows
#	int opApply( int delegate(inout IResultRow) dg );
#}

#interface IResultRow {
#	// this one iterates fields
#	int opApply( int delegate(inout ResultField) dg );
#}

#// As before, since we need to keep track of the field index
#class ResultField {
#	Command cmd;
#	uint fieldno;
#}

#class Command : IResult, IResultRow {	// handle wrapper
#	sqlite3_stmt* stmt;
#	~this() { sqlite3_finalize(stmt); }
#	IResult Execute() { return cast(IResult)this; }
#	// this one iterates rows; does cast(IResultRow)this
#	int IResult.opApply( int delegate(inout IResultRow) dg );
#	// this one iterates fields; does new ResultField(this,fieldno)
#	int IResultRow.opApply( int delegate(inout IResultField) dg );
#}

Or is there a solution to this pattern I haven't thought of yet?

All comments are appreciated :)

L.



More information about the Digitalmars-d-learn mailing list