I have been wondering how Microsoft might add generics to the MS .NET
Framework (hopefully by 2.0).  And I was wondering how they would maintain
source code compatibility with something like ArrayList while adding
ArrayList<T>.  I tried a simple experiment with a brain dead stack class
(both parameterized and not) and I get the following error with the Gyro-
ized Rotor csc compiler:

error CS0104:         'Stack' is an ambiguous reference

The code is listed below.   It seems odd that the compiler would find a
reference to Stack as ambiguous (can't tell if I want Stack or
Stack<int>).  Should this work and the compiler just doesn't handle it
correctly yet or not?

Here is the code:

namespace Collections {
    public class Stack {
        private object[]  m_stack;
        private int       m_ndx = 0;
        private const int s_size = 50;

        public Stack() {
            m_stack = new object[s_size];
        }

        public void Push(object obj) {
            if (m_ndx >= (s_size - 1)) {
                throw new System.Exception("Stack overflow");
            }
            m_stack[m_ndx++] = obj;
        }

        public object Pop() {
            if (m_ndx == 0) throw new System.Exception("Stack underflow");
            return m_stack[--m_ndx];
        }
    }
}

namespace TemplatedCollections {
    public class Stack<T> {
        private T[]       m_stack;
        private int       m_ndx = 0;
        private const int s_size = 50;

        public Stack() {
            m_stack = new T[s_size];
        }

        public void Push(T item) {
            if (m_ndx >= (s_size - 1)) {
                throw new System.Exception("Stack overflow");
            }
            m_stack[m_ndx++] = item;
        }

        public T Pop() {
            if (m_ndx == 0) throw new System.Exception("Stack underflow");
            return m_stack[--m_ndx];
        }
    }
}

namespace Acme {
    using System;
    using Collections;
    using TemplatedCollections;

    class App {
        public static void Main() {
            Stack s1 = new Stack();  // CSC error here
            s1.Push(1);
            s1.Push(4);
            s1.Push((int)s1.Pop() + (int)s1.Pop());
            Console.WriteLine(s1.Pop());

            Stack<int> s2 = new Stack<int>();
            s2.Push(1);
            s2.Push(4);
            s2.Push(s2.Pop() + s2.Pop());
            Console.WriteLine(s2.Pop());
        }
    }
}

Reply via email to