Thread overview |
---|
August 04, 2008 clear initializing constructor | ||||
---|---|---|---|---|
| ||||
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 |
August 04, 2008 Re: clear initializing constructor | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | 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();
|
August 04, 2008 Re: clear initializing constructor | ||||
---|---|---|---|---|
| ||||
Posted in reply to Wyverex | 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();
|
August 04, 2008 Re: clear initializing constructor | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | 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();
>
>
|
August 04, 2008 Re: clear initializing constructor | ||||
---|---|---|---|---|
| ||||
Posted in reply to Wyverex | > > But the static struct has to be filled by constants.. Yeah, not too usefull for my program :) > > 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. |
August 04, 2008 Re: clear initializing constructor | ||||
---|---|---|---|---|
| ||||
Posted in reply to Wyverex | This will work, but is maybe a tad eleborate :) apple=new Fruit("color",RED,"rotten",false); |
August 04, 2008 Re: clear initializing constructor | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | 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); } ..... |
August 04, 2008 Re: clear initializing constructor | ||||
---|---|---|---|---|
| ||||
Posted in reply to Wyverex | 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 .. :) |
Copyright © 1999-2021 by the D Language Foundation