Note that there are some explanatory texts on larger screens.

plurals
  1. POParameterized types and return values in D why does this not work?
    primarykey
    data
    text
    <p>OK, it seems my prior problem is now resolved (I've left it below for reference). However, yet another one crops up - again, seemingly something I'm missing. Further up in this code, I declared the following:</p> <pre><code>private: T _data; BinaryTree!(T) _left; BinaryTree!(T) _right; </code></pre> <p>Then after this, I declared these:</p> <pre><code>public: @property T data() {return _data;} @property BinaryTree!(T) left() {return _left;} @property BinaryTree!(T) right() {return _right;} </code></pre> <p>Then, I implemented the following function:</p> <pre><code>void add (T data) { BinaryTree!(T) nearest = findNearest(this, data); if (nearest.data &gt; data) { nearest.left = new BinaryTree!(T) (data); } else { nearest.right = new BinaryTree!(T) (data); } } </code></pre> <p>The compiler is protesting that nearest.left and nearest.right aren't properties, although as nearest is a BinaryTree!(T), this seems rather odd. What am I missing?</p> <hr> <p>OLD PROBLEM:</p> <p>I've recently gotten into D, and was trying to build a binary tree implementation as a way of testing what I've learned by reading about it. Now, I started by declaring it as follows:</p> <pre><code>class BinaryTree(T) </code></pre> <p>I figured this would allow me to parametrize some of the stuff the class holds. More specifically:</p> <pre><code>T _data; BinaryTree!(T) _left; BinaryTree!(T) _right; </code></pre> <p>Now, I then tried writing this:</p> <pre><code>BinaryTree!(T) findNearest (BinaryTree(T) x, T key) { if (x.hasNoChildren || x.data == key) { return x; //found it } else { auto compare = x.data; if (compare &gt; key) { return find (x.left, key); } else { return find (x.right, key); } } } </code></pre> <p>After this, the compiler <em>loudly</em> complained about my function not having a return type. What am I doing wrong here? If it helps to explain, I come from a Java background, and I'm using the dmd compiler inside of Xamarin Studio.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload