www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Implicit conversion between structs?

reply Steve Teale <steve.teale britseyeview.com> writes:
Is there a way to do this. For example if I have

struct A { int a, b, c; }
struct B
{
   int p, q, r, s;
   this(A a) { p = a.a; q = a.b; r = a.c; }
}

A a;
B b;
b = a;
Oct 31 2011
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 10/31/11 4:43 AM, Steve Teale wrote:
 Is there a way to do this. For example if I have

 struct A { int a, b, c; }
 struct B
 {
     int p, q, r, s;
     this(A a) { p = a.a; q = a.b; r = a.c; }
 }

 A a;
 B b;
 b = a;

Use opAssign? Andrei
Oct 31 2011
next sibling parent reply Steve Teale <steve.teale britseyeview.com> writes:
On Mon, 31 Oct 2011 07:58:11 -0500, Andrei Alexandrescu wrote:

 On 10/31/11 4:43 AM, Steve Teale wrote:
 Is there a way to do this. For example if I have

 struct A { int a, b, c; }
 struct B
 {
     int p, q, r, s;
     this(A a) { p = a.a; q = a.b; r = a.c; }
 }

 A a;
 B b;
 b = a;

Use opAssign?

Joseph (aka Andrei), But if B is not mine to mess with? The two structs have a common type prefix. and D guarantees that there are predictable values for the remainder of the larger one, so the compiler could presumably figure it out. (Easy for me to say) Steve
Oct 31 2011
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
Steve Teale:

 But if B is not mine to mess with?
 
 The two structs have a common type prefix. and D guarantees that there 
 are predictable values for the remainder of the larger one, so the 
 compiler could presumably figure it out. (Easy for me to say)

Looks like a job for StructuralCast!(). Bye, bearophile
Oct 31 2011
prev sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 10/31/11 12:39 PM, Steve Teale wrote:
 On Mon, 31 Oct 2011 07:58:11 -0500, Andrei Alexandrescu wrote:

 On 10/31/11 4:43 AM, Steve Teale wrote:
 Is there a way to do this. For example if I have

 struct A { int a, b, c; }
 struct B
 {
      int p, q, r, s;
      this(A a) { p = a.a; q = a.b; r = a.c; }
 }

 A a;
 B b;
 b = a;

Use opAssign?

Joseph (aka Andrei), But if B is not mine to mess with?

In Soviet Russia everything belongs to the people so you can mess with anything.
 The two structs have a common type prefix. and D guarantees that there
 are predictable values for the remainder of the larger one, so the
 compiler could presumably figure it out. (Easy for me to say)

Well yes, so you could use memcpy, but then that will become a soft error when you change the structs involved. You could take .tupleof for a B object and access its first there members. Generally we could automate a structural cast any way we want but probably we should limit such to only when the names of the members are also identical. Andrei
Oct 31 2011
prev sibling next sibling parent Ali =?iso-8859-1?q?=C7ehreli?= <acehreli yahoo.com> writes:
On Mon, 31 Oct 2011 17:39:57 +0000, Steve Teale wrote:

 On Mon, 31 Oct 2011 07:58:11 -0500, Andrei Alexandrescu wrote:
 
 On 10/31/11 4:43 AM, Steve Teale wrote:
 Is there a way to do this. For example if I have

 struct A { int a, b, c; }
 struct B
 {
     int p, q, r, s;
     this(A a) { p = a.a; q = a.b; r = a.c; }
 }

 A a;
 B b;
 b = a;

Use opAssign?

But if B is not mine to mess with?

The compiler has the same problem.
 The two structs have a common type prefix.

Who can know that the first part of B can be used as an A? Three ints may be coordinate values in a system, or they could be shoe sizes of a three- legged sheep.
 and D guarantees that there
 are predictable values for the remainder of the larger one, so the
 compiler could presumably figure it out.

The compiler should not make such high level inferences. I would like to receive a compilation error when assignment of unrelated types are unsupported as in the case above. Ali
Oct 31 2011
prev sibling parent Steve Teale <steve.teale britseyeview.com> writes:
OK, I stand corrected 

Steve
Oct 31 2011