Mixins templates would be the answer:<div>import std.exception;
</div><div><div><br></div><div>mixin template UncheckedIndices( T ) {</div><div> ref T opIndex( int i ) {</div><div> return this.get_( i );</div><div> }</div><div> </div><div>}</div><div><br></div><div>mixin template CheckedIndices( T ) {</div>
<div> ref T opIndex( int i ) {</div><div> enforce( i > 0 && i < this.size );</div><div> return this.get_( i );</div><div> }</div><div>}</div><div><br></div><div>struct Array( T, alias IndexPolicy ) {</div>
<div> private T* array;</div><div> private int size_;</div><div> </div><div> this( size_t n ) {</div><div> array = (new T[ n ]).ptr;</div><div> }</div><div><br></div><div> @property size_t size() {</div><div> return size_;</div>
<div> }</div><div><br></div><div> private ref T get_( size_t i ) {</div><div> return array[ i ];</div><div> }</div><div><br></div><div> mixin IndexPolicy!T;</div><div>}</div><div><br></div><div>void main() {</div><div>
<span class="Apple-tab-span" style="white-space:pre"> </span>auto arr1 = Array!(int, UncheckedIndices)( 10 );</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>auto arr2 = Array!(int, CheckedIndices)( 10 );</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>arr2[ 11 ] = 5; </div><div>}</div></div><div><br></div><div><br></div><div><br clear="all">
<div><br></div><div>---</div>Cristi Cobzarenco<div>BSc in Artificial Intelligence and Computer Science</div><div>University of Edinburgh<br>Profile: <a href="http://www.google.com/profiles/cristi.cobzarenco" target="_blank">http://www.google.com/profiles/cristi.cobzarenco</a></div>
<br>
<br><br><div class="gmail_quote">On 3 April 2012 00:15, Joseph Rushton Wakeling <span dir="ltr"><<a href="mailto:joseph.wakeling@webdrake.net">joseph.wakeling@webdrake.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hello all,<br>
<br>
I'm coming to D from a background programming in C and C++, though I wouldn't describe myself as an expert in either.<br>
<br>
One of the C++ techniques I picked up over the last couple of years was the use of policy classes, and I'm wondering how D addresses this issue of combining various small components together to implement a given interface.<br>
<br>
D's interfaces seem an obvious starting point, but from the documentation I've read, it seems like each implementation has to be written separately. So, if I have an interface,<br>
<br>
interface FooBar {<br>
void foo();<br>
void bar();<br>
}<br>
<br>
... I can of course write two different implementations,<br>
<br>
class FooBarOne : FooBar {<br>
override void foo() {<br>
// Foo function implementation<br>
...<br>
}<br>
override void bar() {<br>
// Bar function implementation<br>
...<br>
}<br>
}<br>
<br>
class FooBarTwo : FooBar {<br>
override void foo() {<br>
// Foo function implementation<br>
...<br>
}<br>
override void bar() {<br>
// Bar function implementation<br>
...<br>
}<br>
}<br>
<br>
... but suppose that I'd like the foo() function to be identical in both; how do I avoid rewriting the code?<br>
<br>
In C++ I'd think of a policy class,<br>
<br>
template <class Foo, class Bar><br>
class FooBar : public Foo, public Bar {<br>
...<br>
};<br>
<br>
and then have,<br>
<br>
typedef FooBar<FooGeneric,BarOne> FooBarOne;<br>
<br>
typedef FooBar<FooGeneric,BarTwo> FooBarTwo;<br>
<br>
... but I don't see how to do something equivalent with D's interfaces and implementations. Can anyone advise?<br>
<br>
Thanks and best wishes,<br>
<br>
-- Joe<br>
</blockquote></div><br></div>