[e-lang] Generators for the other Joe-E array types

Tyler Close tyler.close at gmail.com
Fri Dec 14 12:40:52 EST 2007


I think we need generators, like the recently discussed
ByteArray.Generator class, for all the other Joe-E array types as
well. In the absence of such generators, I haven't been able to find a
convenient syntax for expressing creation of Joe-E arrays. All the
patterns I've tried result in unchecked cast warnings from javac, as
well as unnecessary array copies. For example, consider the following
code taken from the org.waterken.menu.Menu implementation in the
Waterken Server:

        class MenuX implements Menu<T>, Serializable {
            static private final long serialVersionUID = 1L;

            private ConstArray<Variable<T>> editors = array();

            public Promise<ConstArray<T>>
            getSnapshot() {
                final T[] r = alloc(editors.length());
                int i = 0;
                for (final Variable<T> x : editors) { r[i++] = x.get(); }
                return ref(array(r));
            }

            @SuppressWarnings("unchecked") private T[]
            alloc(final int length) { return (T[])new Object[length]; }

            ...

The getSnapshot() method wants to return a ConstArray containing the
current values taken from a list of variables. The type of value held
in each variable is generic.

I think we need something like:

    interface Generator<T> {

        void append(T value);

        ConstArray<T> snapshot();
    }

Each of the Joe-E array types would then provide an implementation of
this interface. For convenience, each Joe-E array type should also
provide a static method that constructs one of these generators, so:

    static public Generator<T> generate(int estimate);

The Menu.getSnapshot() implementation would then become:

            public Promise<ConstArray<T>>
            getSnapshot() {
                final Generator<T> g = generate(editors.length());
                for (final Variable<T> x : editors) { g.append(x.get()); }
                return ref(g.snapshot());
            }

Simpler and more efficient.

--Tyler

-- 
The web-calculus is the union of REST and capability-based security:
http://www.waterken.com/dev/Web/

Name your trusted sites to distinguish them from phishing sites.
https://addons.mozilla.org/firefox/957/


More information about the e-lang mailing list