www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - overloadable implicit cast

reply =?ISO-8859-1?Q?Adam_Cig=E1nek?= <adam.ciganek gmail.com> writes:
Hello,

Is it possible to make a type implicitly cast to another type?
Something like overloading opCast, but make it so it's used
implicitly. Or is there implicit casting in D at all?

Pseudo-example:

  // hypothetical implicit cast:
  A opImplicitCast(T)(B b) if (is(T == A)) {
    // convert b to A somehow;
  }

  void do_stuff(A a) {
    // do some stuff with a;
  }

  A a;
  B b;

  do_stuff(b); // equivalent to do_stuff(opImplicitCast(A)(b));


adam.
Oct 26 2010
parent Jesse Phillips <jessekphillips+D gmail.com> writes:
Adam Cigánek Wrote:

 Hello,
 
 Is it possible to make a type implicitly cast to another type?
 Something like overloading opCast, but make it so it's used
 implicitly. Or is there implicit casting in D at all?
OpImplicitCast was planed to be added, but it has been superseded by alias this. Due to many bugs related to alias this, it is mostly unusable.
Oct 26 2010