www.digitalmars.com         C & C++   DMDScript  

D - implicit conversions

reply "Carlos Santander B." <carlos8294 msn.com> writes:
This has been touched before, but anyway...

Is there a way to get this kind of behavior in D?

class Complex {
        private double re,im;
        this() { this(r); }
        this(double r) { this(r,0); }
        this(double r,double i) { re=r; im=i; }
}

void main() {
        Complex x = new Complex();
        x=4; //this
        x=cast(Complex)4; //or maybe even this
}

I know it can be done explicitly calling the appropiate constructor, but I'd
like to also do it like this.

-------------------------
Carlos Santander
This has been touched before, but anyway...

Is there a way to get this kind of behavior in D?

class Complex {
        private double re,im;
        this() { this(r); }
        this(double r) { this(r,0); }
        this(double r,double i) { re=r; im=i; }
}

void main() {
        Complex x = new Complex();
        x=4; //this
        x=cast(Complex)4; //or maybe even this
}

I know it can be done explicitly calling the appropiate constructor, but I'd
like to also do it like this.

-------------------------
Carlos Santander
Oct 16 2003
parent "Walter" <walter digitalmars.com> writes:
"Carlos Santander B." <carlos8294 msn.com> wrote in message
news:bmmrol$17vr$1 digitaldaemon.com...
 This has been touched before, but anyway...

 Is there a way to get this kind of behavior in D?

 class Complex {
         private double re,im;
         this() { this(r); }
         this(double r) { this(r,0); }
         this(double r,double i) { re=r; im=i; }
 }

 void main() {
         Complex x = new Complex();
         x=4; //this
         x=cast(Complex)4; //or maybe even this
 }

 I know it can be done explicitly calling the appropiate constructor, but
I'd
 like to also do it like this.
At the moment there isn't a way, but perhaps this should be dealt with.
Oct 27 2003