On 29/06/2010 00:07, Simen kjaeraas wrote:
Maybe it's because you haven't added string to the setter? Just grasping
at straws here.

--
Simen

Hi Simen, yes, thats the prob. I just have not found the @property docs.. thanks for all the help..
This snippet works now as expected.. D properties are just fine.
Bjoern
import std.stdio;
import std.random;

void main(string[] args)
{
        LoadBalancer b1 = LoadBalancer.getLoadBalancer();
        LoadBalancer b2 = LoadBalancer.getLoadBalancer();
        LoadBalancer b3 = LoadBalancer.getLoadBalancer();

        // Confirm these are the same instance
    if (b1 == b2 && b2 == b3 )  {
        writeln("Same instance\n");
        }

        // Next, load 15 requests for a server
        for (int i = 0; i < 15; i++) {
                string serverName = b1.nextServer.servername;
                writeln("Dispatch request to: " ~ serverName);
      }

}

// D2 thread safe ?????? singleton

final class LoadBalancer {
        private static LoadBalancer lb;
        private Server[] servers;
        
        static this() {
                synchronized lb = new LoadBalancer;
        }
        
        private this() {
                Server s1 = new Server();
                s1.servername = "Server 1";
                s1.id = "";
                Server s2 = new Server();
                s2.servername = "Server 2";
                servers ~= s1;
                servers ~= s2;

        }
        
        public static LoadBalancer getLoadBalancer() {
      return lb;
    }

        @property
        {       
                Server nextServer() { return servers[ uniform(0,        
servers.length) ]; }
        }

        // inner class  
        class Server {
                private string _name, _id;

                @property
                {
                        string servername(string sn) { return _name = sn; }
                        string servername() { return _name;     }

                        string id(string id) { return _id = id; }
                        string id() { return _id; }
                }
        }
}

Reply via email to