www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - clear initializing constructor

reply "Saaa" <empty needmail.com> writes:
How do you do something like this?

class Fruit
{

protected struct _Eigen
{
byte color = GREEN;
bool rotten = false;
}
private _Eigen eigen;

this(...)
{
 // ?
}

}

apple=new Fruit(color=RED,rotten=false);
apple2=new Fruit(rotten=true);
apple3=new Fruit(pit=false); //pit=ignored
Aug 04 2008
parent reply Wyverex <wyverex.cypher gmail.com> writes:
Saaa wrote:
 How do you do something like this?
 
 class Fruit
 {
 
 protected struct _Eigen
 {
 byte color = GREEN;
 bool rotten = false;
 }
 private _Eigen eigen;
 
 this(...)
 {
  // ?
 }
 
 }
 
 apple=new Fruit(color=RED,rotten=false);
 apple2=new Fruit(rotten=true);
 apple3=new Fruit(pit=false); //pit=ignored
 
 
 

class Fruit { protected struct _Eigen { byte color = GREEN; bool rotten = false; } private _Eigen eigen; this() { } this(bool rotten, byte color) { eigen.rotten = rotten; eigen.color = color; } } auto apple = new Fruit( false, RED ); auto normal = new Fruit();
Aug 04 2008
parent reply "Saaa" <empty needmail.com> writes:
Thanks for your reply.
But I think  I wasn't clear in my question.
As _Eigen can get quite large I think it is necessary to have something 
like:

apple=new Fruit(color=RED,rotten=false);

otherwise thinks like this will happen:

apple=new Fruit(,,,RED,,false,,true etc.);





 class Fruit
 {

    protected struct _Eigen
    {
       byte color = GREEN;
       bool rotten = false;
    }

     private _Eigen eigen;

     this()
     {
     }

     this(bool rotten, byte color)
     {
          eigen.rotten = rotten;
          eigen.color = color;
     }
 }

 auto apple = new Fruit( false, RED );
 auto normal = new Fruit(); 

Aug 04 2008
parent reply Wyverex <wyverex.cypher gmail.com> writes:
Not directly you can do something like

struct test
{
   bool A;
   int B;
   float C;
}

class foo
{
   private test t;

   this(test bar)
   {
    t = bar;
   }
}

void main()
{
   static test t = { A:true, C:4.5 };
   foo f = new foo( t);

}

But the static struct has to be filled by constants..

But it sounds like you might be better off using inheritance..
create a base Fruit class that you inherit and modify needed data..




Saaa wrote:
 Thanks for your reply.
 But I think  I wasn't clear in my question.
 As _Eigen can get quite large I think it is necessary to have something 
 like:
 
 apple=new Fruit(color=RED,rotten=false);
 
 otherwise thinks like this will happen:
 
 apple=new Fruit(,,,RED,,false,,true etc.);
 
 
 
 
 
 class Fruit
 {

    protected struct _Eigen
    {
       byte color = GREEN;
       bool rotten = false;
    }

     private _Eigen eigen;

     this()
     {
     }

     this(bool rotten, byte color)
     {
          eigen.rotten = rotten;
          eigen.color = color;
     }
 }

 auto apple = new Fruit( false, RED );
 auto normal = new Fruit(); 


Aug 04 2008
next sibling parent "Saaa" <empty needmail.com> writes:
 But the static struct has to be filled by constants..

 But it sounds like you might be better off using inheritance..
 create a base Fruit class that you inherit and modify needed data..

Will that not take just as much code as modifying the needed data within the objects? Or aren't I getting it :) All permutations of possible settings should be possible.
Aug 04 2008
prev sibling parent reply "Saaa" <empty needmail.com> writes:
This will work, but is maybe a tad eleborate :)

apple=new Fruit("color",RED,"rotten",false);
Aug 04 2008
parent reply Wyverex <wyverex.cypher gmail.com> writes:
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); } .....
Aug 04 2008
parent "Saaa" <empty needmail.com> writes:
Thanks, I was looking at mixin myself but the compile time evaluating didn't 
work for me.
I totally forgot about 'with', might at least make it more compacted 
together.

I just wanted it like in R .. :) 
Aug 04 2008