dcollections how to LinkList // port c# code

Steven Schveighoffer schveiguy at yahoo.com
Tue Jun 29 13:12:37 PDT 2010


On Tue, 29 Jun 2010 15:22:30 -0400, BLS <windevguy at hotmail.de> wrote:

> Hi,
> in C# this is  common.
>
> private List<Server> _servers;
>   _servers = new List<Server>
>          {
>           new Server{ Name = "ServerI", IP = "120.14.220.18" },
>           new Server{ Name = "ServerII", IP = "120.14.220.19" },
>           new Server{ Name = "ServerIII", IP = "120.14.220.20" },
>           new Server{ Name = "ServerIV", IP = "120.14.220.21" },
>           new Server{ Name = "ServerV", IP = "120.14.220.22" },
>          };
>
> D2 so far..
> import dcollections.LinkList;
> class LoadBalancer {
> 	alias LinkList!Server ServerList;
> 	private ServerList sl;	
> 	
> 	this() {
> 		sl = new ServerList;
> 		sl.add( new Server() );
>
> ...
> }
>
> Do I really have to create something like this
> auto x = new Server(); x.Name = "Blah"; x.IP = "120.14.220.22";
> s1.add(x)
> (Name and IP are Server properties.)

I think I need to add some constructors that accept data.  std.container  
has some cool construction methods.

For now, can you do something like this?

sl = new ServerList;
sl.add([
    new Server("ServerI", "120.14.220.18"),
    new Server(...)
    ...
]);

The new constructor would probably do something like this:

sl = new ServerList(
     new Server(...),
     new Server(...),
     ...
);

Does that work for you?  If you need to build servers by naming fields,  
I'm not sure that's really a dcollections issue, D doesn't support  
constructing object by specifing individual field names.  Alternatively,  
you could define an external constructor:

Server create(string name, string ip)
{
    auto retval = new Server();
    retval.Name = name;
    retval.IP = ip;
    return retval;
}

BTW, I don't think I've ever constructed a list that way in C#, it's cool  
:)

-Steve


More information about the Digitalmars-d-learn mailing list