clear initializing constructor

Wyverex wyverex.cypher at gmail.com
Mon Aug 4 15:30:22 PDT 2008


Saaa wrote:
> This will work, but is maybe a tad eleborate :)
> 
> apple=new Fruit("color",RED,"rotten",false);
> 
> 
> 
> 

Two more thoughts...

Use with. not as clean looking and requires write access to data
http://www.digitalmars.com/d/1.0/statement.html#WithStatement

Foo = new Fruit;
with(Foo)
{
   rotten = true;
   color = YELLOW;
   taste = SWEET;
   bitesLeft = nBites; //variable
}


the other is a mixin, I've never really messed with them but something 
like below..  But the string has to be evaluated at compile time...
http://www.digitalmars.com/d/1.0/statement.html#MixinStatement


badBerry = new Fruit( "rotten = true; color = BLUE" );

class Fruit
{

    this(const char[] type)
    {
      mixin(type);
    }

.....



More information about the Digitalmars-d-learn mailing list