D - How to instantiate a template? Bug?
- Andres Rodriguez (94/102) Feb 01 2004 Here is a minimalist ArrayList implementation using templates. Everythi...
- Ivan Senji (10/10) Feb 01 2004 I had the exact same problem with a class "set" i tried to write!
- Ilya Zaitsev (14/24) Feb 01 2004 But this works:
- Walter (15/118) Jun 06 2004 The trouble you're having is that class templates declare two symbols, a
Here is a minimalist ArrayList implementation using templates. Everything compiles fine, until I throw the second test file in the mix. I tried using the same syntax from the Christopher E. Miller example of a linked list. The trouble seems to be the method 'addAll', because if I comment it out, eveything compiles. But the weird thing is that MyArrayList compiles fine without the test file at the end, is only after adding that file that I get the compilation error. I get the following error:cd c:/dev/d/util/src/sri/util/test/ make -k -f c:/dev/d/util/Makefile compile \dmd\bin\dmd -c -od\dev\d\util\bin -I\dev\d\util\src -release\dev\d\util\src\sri\util\MyArrayList\dmd\bin\dmd -c -od\dev\d\util\bin -I\dev\d\util\src -release\dev\d\util\src\sri\util\test\TestMyArrayListtemplate instance MyArrayList!(char[]) MyArrayList is not a templatedeclarationmake: *** [sri-util-test] Error 1 make: Target `compile' not remade because of errors. Compilation exited abnormally with code 2 at Sun Feb 01 08:40:29Thanks in advance, Andres ***** File MyArrayList ******************************************************* module java.util.MyArrayList; public class MyArrayList(T) { //~ ATTRIBUTE(S) --------------------------------------------------------- private int length = 0; private T[] data; //~ CONSTRUCTOR(S) ------------------------------------------------------- public this() { } public this(MyArrayList!(T) c) { addAll(c); } public this(int initialCapacity) { data.length = initialCapacity; } //~ METHOD(S) ------------------------------------------------------------ public boolean add(T o) { ensureCapacity(length+1); data[length++] = o; return true; } public void add(int index, T element) { ensureCapacity(length+1); for (int i = length - 1; i > index; i--) data[i] = data[i-1]; data[index] = element; } public boolean addAll(MyArrayList!(T) c) { boolean modified = false; for (int i = 0; i < c.length; i++) { if (add(c.get(i))) modified = true; } return modified; } public void clear() { data.length = 0; } public boolean contains(T o) { for (int i = 0; i < length; i++) if (o == data[i]) return true; return false; } public void ensureCapacity(int minCapacity) { if (minCapacity > data.length) data.length = (data.length == 0? 10 : 2 * data.length); } public T get(int index) { return data[index]; } public boolean isEmpty() { return length == 0; } public T remove(int index) { T o = data[index]; length--; for (int i = index; i < length; i++) data[i] = data[i+1]; return o; } public boolean remove(T o) { int lag = 0; for (int i = 0; i < length - lag; i++) { if (o == data[i]) lag++; if (lag > 0) data[i] = data[i+lag]; } length -= lag; return lag > 0; } public T set(int index, T element) { return data[index] = element; } public int size() { return length; } } ***** File TestMyArrayList *************************************************** module sri.util.test.TestMyArrayList; import sri.util.MyArrayList; public class TestMyArrayList { protected void buildArrayList() { MyArrayList!(char[]) al = new MyArrayList!(char[]); al.add("a"); al.add("c"); al.add("u"); al.add("n"); al.add("i"); al.add("a"); al.add(null); } }
Feb 01 2004
I had the exact same problem with a class "set" i tried to write! i had class set(T) { ... void add(set!(T) a) { } //this is the member function that causes compiler error! }
Feb 01 2004
On Sun, 1 Feb 2004 20:35:20 +0100, Ivan Senji <ivan.senji public.srce.hr> wrote:I had the exact same problem with a class "set" i tried to write! i had class set(T) { ... void add(set!(T) a) { } //this is the member function that causes compiler error! }But this works: template TSet(T) { class Set { void add(TSet!(T).Set item) { } } } int main() { TSet!(int).Set set = new TSet!(int).Set(); set.add(set); return 0; } I think, it's a bug in templates with a new style syntax.
Feb 01 2004
The trouble you're having is that class templates declare two symbols, a template symbol and its associated class symbol. Both have the same name. Inside the scope of the class template instantiation, using the class template name resolves to the class symbol, not the template symbol. Thus, replacing: public boolean addAll(MyArrayList!(T) c) with: public boolean addAll(MyArrayList c) should resolve it. If you want to use the template symbol, add the module scope '.' operator in front: public boolean addAll(.MyArrayList!(T) c) "Andres Rodriguez" <rodriguez ai.sri.com> wrote in message news:bvjbal$4t6$1 digitaldaemon.com...Here is a minimalist ArrayList implementation using templates. Everything compiles fine, until I throw the second test file in the mix. I tried using the same syntax from the Christopher E. Miller example of a linked list. The trouble seems to be the method 'addAll', because if I comment it out, eveything compiles. But the weird thing is that MyArrayList compiles fine withoutthetest file at the end, is only after adding that file that I get the compilation error. I get the following error::cd c:/dev/d/util/src/sri/util/test/ make -k -f c:/dev/d/util/Makefile compile \dmd\bin\dmd -c -od\dev\d\util\bin -I\dev\d\util\src -release\dev\d\util\src\sri\util\MyArrayList\dmd\bin\dmd -c -od\dev\d\util\bin -I\dev\d\util\src -release\dev\d\util\src\sri\util\test\TestMyArrayListtemplate instance MyArrayList!(char[]) MyArrayList is not a templatedeclarationmake: *** [sri-util-test] Error 1 make: Target `compile' not remade because of errors. Compilation exited abnormally with code 2 at Sun Feb 01 08:40:29Thanks in advance, Andres ***** File MyArrayList ******************************************************* module java.util.MyArrayList; public class MyArrayList(T) { //~ ATTRIBUTE(S) --------------------------------------------------------- private int length = 0; private T[] data; //~ CONSTRUCTOR(S) ------------------------------------------------------- public this() { } public this(MyArrayList!(T) c) { addAll(c); } public this(int initialCapacity) { data.length = initialCapacity; } //~ METHOD(S) ------------------------------------------------------------ public boolean add(T o) { ensureCapacity(length+1); data[length++] = o; return true; } public void add(int index, T element) { ensureCapacity(length+1); for (int i = length - 1; i > index; i--) data[i] = data[i-1]; data[index] = element; } public boolean addAll(MyArrayList!(T) c) { boolean modified = false; for (int i = 0; i < c.length; i++) { if (add(c.get(i))) modified = true; } return modified; } public void clear() { data.length = 0; } public boolean contains(T o) { for (int i = 0; i < length; i++) if (o == data[i]) return true; return false; } public void ensureCapacity(int minCapacity) { if (minCapacity > data.length) data.length = (data.length == 0? 102 * data.length); } public T get(int index) { return data[index]; } public boolean isEmpty() { return length == 0; } public T remove(int index) { T o = data[index]; length--; for (int i = index; i < length; i++) data[i] = data[i+1]; return o; } public boolean remove(T o) { int lag = 0; for (int i = 0; i < length - lag; i++) { if (o == data[i]) lag++; if (lag > 0) data[i] = data[i+lag]; } length -= lag; return lag > 0; } public T set(int index, T element) { return data[index] = element; } public int size() { return length; } } ***** File TestMyArrayList *************************************************** module sri.util.test.TestMyArrayList; import sri.util.MyArrayList; public class TestMyArrayList { protected void buildArrayList() { MyArrayList!(char[]) al = new MyArrayList!(char[]); al.add("a"); al.add("c"); al.add("u"); al.add("n"); al.add("i"); al.add("a"); al.add(null); } }
Jun 06 2004