Implicit conversion between structs?

Ali Çehreli acehreli at yahoo.com
Mon Oct 31 13:30:11 PDT 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:
> 
>> 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 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


More information about the Digitalmars-d mailing list