Using templates with interfaces

Andrew Chapman via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 27 15:06:37 PDT 2017


On Sunday, 25 June 2017 at 17:30:58 UTC, Petar Kirov [ZombineDev] 
wrote:
> On Sunday, 25 June 2017 at 13:32:57 UTC, Andrew Chapman wrote:
>> I think you've answered the question with "You cannot have 
>> unimplemented templates in interfaces".  Thanks for the answer.
>>  I'll rethink the way I'm doing this.
>>
>> Cheers.
>
> In your case you can probably use something along the lines of:
>
> interface RelationalDBInterface
> {
>     // You can even make this protected
>     Varaint loadUntypedRow(string sql, Variant[] params);
>
>     final T loadRow(T)(string sql, Variant[] params)
>     {
>         auto row = loadUntypedRow(sql, params);
>
>         enforce(row.hasValue,
>             this.classID ~ "::loadRow - Query returned an empty 
> row");
> 	
> 	return row.toStruct!T;
>     }
> }

Amazing, thank you!


More information about the Digitalmars-d-learn mailing list