Safer casts

Janice Caron caron800 at googlemail.com
Sun May 11 09:17:26 PDT 2008


On 11/05/2008, Yigal Chripun <yigal100 at gmail.com> wrote:
> > Umm. Yes.
>  >
>  >     auto col = new LinkedList!(T);
>  >     sort(col);
>  >
>  > becomes
>  >
>  >     auto col = new Vector!(T);
>  >     sort(col);
>  >
>
> right...
>  and what about my function?
>  void WalkListAndDoSomething(List!(T) list);
>   would become what?

    List!(T) list;
    map!(doSomething)(list);

or

    List!(T) list;
    reduce!(doSomething)(x,list);

or

    List!(T) list;
    inPlace!(doSomething)(list);

depending on which was more appropriate for doSomething.



>  void WalkListAndDoSomething(auto list);
>  I wonder if the compiler will accept that..

I'm baffled as to why you even said that. You know it won't. It's silly.


>  There is no such thing as List!(T) in the STL design. either you use a
>  vector or you use a linked list. there's nothing in between.

I thought we were talking D here? I took your use of "List!(T)" there
to mean any arbitrary collection.


> But you are re-inventing the wheel!

No I'm not.


> the entire point is

Again, you're telling me there's this "the" point, which is different
from "a" point, and somehow more important than any point I might
make.


> to have a
> collection framework in the standard library.

Again, you're criticising today's D, and failing to acknowledge that
tomorrow's D may (and almost certainly will) have a collection
framework in the standard library.



> the overhead [of iterators] is a separate object, and function calls for the
> iterator object.

By "object" you presumably mean struct - often times small enough to
pass around in a register. There is essentially no difference between
a struct containing a pointer, and a pointer.

By "function" you presumably mean static function, which takes zero
space in the struct, and will almost certainly be inlined when
compiled.

Overhead gone.



> for most simple traversals, that's an unnecessary
> overhead when I could just use the collection.each method with a
> delegate

Now that /does/ have a function call overhead.


>  if I just want
>  to sum the values of all the objects in the collection, than an iterator
>  is an overhead.

Or you could just do

    int sum = reduce!("a+b")(0,collection);

:-)


>  besides, all those templates could double-up as code the collection
>  writers can mix-in into their collection classes to implement the
>  interfaces.

Then aren't we lucky they exist! :-)


>  that's a win-win situation.

Yes.



More information about the Digitalmars-d mailing list