digitalmars.D - Implicit conversion between structs?
- Steve Teale (10/10) Oct 31 2011 Is there a way to do this. For example if I have
- Andrei Alexandrescu (3/13) Oct 31 2011 Use opAssign?
- Steve Teale (7/23) Oct 31 2011 Joseph (aka Andrei),
- bearophile (4/9) Oct 31 2011 Looks like a job for StructuralCast!().
- Ali =?iso-8859-1?q?=C7ehreli?= (9/35) Oct 31 2011 Who can know that the first part of B can be used as an A? Three ints ma...
- Andrei Alexandrescu (9/31) Oct 31 2011 In Soviet Russia everything belongs to the people so you can mess with
- Steve Teale (2/2) Oct 31 2011 OK, I stand corrected
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
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
On Mon, 31 Oct 2011 07:58:11 -0500, Andrei Alexandrescu wrote:On 10/31/11 4:43 AM, Steve Teale wrote: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) SteveIs 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?
Oct 31 2011
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
On Mon, 31 Oct 2011 17:39:57 +0000, Steve Teale wrote:On Mon, 31 Oct 2011 07:58:11 -0500, Andrei Alexandrescu wrote:The compiler has the same problem.On 10/31/11 4:43 AM, Steve Teale wrote:Joseph (aka Andrei), But if B is not mine to mess with?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?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
On 10/31/11 12:39 PM, Steve Teale wrote:On Mon, 31 Oct 2011 07:58:11 -0500, Andrei Alexandrescu wrote:In Soviet Russia everything belongs to the people so you can mess with anything.On 10/31/11 4:43 AM, Steve Teale wrote:Joseph (aka Andrei), But if B is not mine to mess with?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?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