On 2010-05-20 09:22:27 -0400, Steven Schveighoffer <[email protected]> said:

Michel Fortin Wrote:

On 2010-05-20 06:34:42 -0400, Steven Schveighoffer <[email protected]> said:

I understand these points, but I'm already using interfaces to copy
data between containers.  I don't have to, I could have used generic
code, but this way, only one function is instantiated to copy data from
all the other containers.  The problem with using generic code is that
the compiler will needlessly duplicate functions that are identical.

One question. Have you calculated the speed difference between using an
interface and using generic code? Surely going through all those
virtual calls slows things down a lot.

I do like interfaces in principle, but I fear it'll make things much
slower when people implement things in term of interfaces. That's why
I'm not sure it's a good idea to offer container interfaces in the
standard library.

It's not that much slower. You get a much higher speedup when things can be inlined than virtual vs. non-virtual.

Yes, but that was part of the equation: a call to a template function can be inlined, not a virtual call (most of the time).

However, I should probably make all the functions in the concrete implementations final. I made several of them final, but I should do it across the board.

Yeah, probably.

One thing I just thought of -- in dcollections, similar types can be compared to one another. For example, you can check to see if a HashSet is equal to a TreeSet. But that would not be possible without interfaces.

I'm not sure of what you're saying here. Are you saying it can be done with an interface but not with a template type? Why can't this work:

        class TreeSet {
                bool opEquals(T)(T t) if (IsSet!T) {
                        if (t.length != this.length)
                                return false;
                        foreach (element; this) {
                                if (!t.has(element))
                                        return false;
                        }
                        return true;
                }
        }

        TreeSet a = new TreeSet;
        HashSet b = new HashSet;
        assert(a == b);

(sorry, I still have to start using the new operator overloading syntax.)

--
Michel Fortin
[email protected]
http://michelf.com/

Reply via email to