On Sun, Sep 2, 2012 at 12:39 AM, Philippe Sigaud <span dir="ltr"><<a href="mailto:philippe.sigaud@gmail.com" target="_blank">philippe.sigaud@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im"><p><br>
> To make a type "double linkable" the developer needs to mixin the following mixin template:<br>
><br>
>> mixin template DoubleLinkable()<br>
>> {<br>
>> typeof(this) next;<br>
>> typeof(this) prev;<br>
>> }<br>
><br>
><br>
> The next and prev pointer can be access with the help of mixin by using the following templates:<br>
><br>
>> T* next(T, string name)(T node) pure nothrow const <br>
>><br>
>> { <br>
>><br>
>> mixin("return &(node." ~ name ~ ".next);"); <br>
>><br>
>> } </p>
</div><p>These are free functions? Why not put them in the mixin templates?<br></p></blockquote><div><br></div><div>They are actually part of the DoubleLinkedList template but I put them as free functions because it is easier to read. My motivation for putting them in the DoubleLinkedList template and class is because they are part of the Container logic. For example they may need to be synchronized if you want a concurrent data structure.</div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><p></p><div class="im">
<p>> To use the above abstraction the developer just needs to do the following:<br>
><br>
>> class Person<br>
>> {<br>
>> int age;<br>
>> int weight;<br>
>> mixin DoubleLinkable people;<br>
>> } </p>
</div><p>Oh, named mixins! Yet another D feature I totally forgot. Hey, those reading this, who here knew you could do 'mixin someTemplate someName;' to refer to the template scope by the given name?<br></p><div class="im">
<p>> I am not a big fan of the template signature 'class DoubleLinkedList(T, string name)' but I couldn't figure out a better way of allowing one object to be embedded in multiple containers. Thoughts? Any idea how this can be improved so that it is easier to use and read?</p>
</div><p>I didn't read the blog post, but any reason why you do not put all the list machinery in a mixin template and refer directly to next/prev?</p></blockquote><div>I think I answer this above. </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<p>I'm on a pad, so typing is not so easy, but like this:</p>
<p>mixin template NodeImplementation()<br>
{<br>
// should detect if typeof(this) is a reference type or a value type.<br>
// for a reference type, we should not use pointers.<br>
typeof(this) next() @property { return *_next;}<br>
typeof(this) prev() @property { return *_prev;}<br>
/// + setters also<br>
private *typeof(this) _next,_prev;<br>
}</p>
<p>struct Person()<br>
{<br>
mixin NodeImplementation;<br>
string name;<br>
int age;<br>
}</p>
<p>And DoublyLinkedList(Node) if (isNode!Node)</p>
<p>with</p>
<p>/** <br>
A node is a type T that has .next and .prev fields<br>
that return another T. <br>
*/ <br>
isNode(T)<br>
{<br>
enum isNode = __traits(compiles, { T p = T.init.prev; T n = T.init.next;});<br>
}</p></blockquote><div><br></div><div>Yep. DoubleLinkedList should have a template constraint but I didn't want to add to many D features as I plan to post this as a comment in the blog. All the C/C++ would have a harder time understanding the D code. ;)</div>
<div><br></div><div>Thanks!</div><div>-Jose</div></div>