Hey<div><br></div><div>I've very recently started playing around with D, but I seem to have hit my head against a bug in dmd. I want to create a Vector class templated with its dimension. This works fine until I try to overload the + operator for my vector and use it. I then get the error </div>
<div><br></div><div>Error: incompatible types for ((vec) + (vec)): 'Vector!(3)' and 'Vector!(3)'</div><div><br></div><div>I've boiled it down to a small example</div><div><br></div><div><div>struct Vector(int D) {</div>
<div> float es[D];</div><div> </div><div>public:</div><div> this(immutable float x, immutable float y, immutable float z) {</div><div> es[0] = x; es[1] = y; es[2] = z;</div><div> }</div><div> </div><div>
Vector!(D) opBinary(string s)(immutable Vector!(D) rhs) if (s == "+") { </div><div> Vector!(D) ret;</div><div> for(int i = 0; i < D; ++i)</div><div> <a href="http://ret.es">ret.es</a>[i] = es[i] + <a href="http://rhs.es">rhs.es</a>[i];</div>
<div> return ret; </div><div> }</div><div> </div><div>}</div><div><br></div><div>alias Vector!(3) Vector3;</div><div><br></div><div>void main() {</div><div> Vector3 vec = Vector3(0,1,2);</div><div> vec = vec.opBinary!("+")(vec); // This works fine</div>
<div> vec = vec + vec; // This line fails miserably</div><div>}</div></div><div><br></div><div>If I replace the template argument in for the argument rhs with 3, then everything works, but that's not really a nice solution :)</div>
<div><br></div><div>Is it me trying to use templates in a way they aren't meant to be used or is this a bug in dmd? If it is a bug, does anyone have an idea how to solve this? I wouldn't mind fixing it in dmd myself, I just need some guidelines.</div>
<div><br></div><div>/papaboo</div><div><br>-- <br>I confess<br>---<br>From the desk of Dr. John Zoidberg, M.D. <div>(\/)(',,,')(\/)</div><br>
</div>