Implementing casting outside of the target struct

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Mar 16 15:37:41 PDT 2017


On Thu, Mar 16, 2017 at 09:36:31PM +0000, David Zhang via Digitalmars-d-learn wrote:
> Hi,
> 
> I have two structs and want to cast between them. The cast is
> generalizable, so I want to keep it outside of the structs themselves
> (I'll be adding more later). How can I do this?

AFAIK, the opXXX() methods must be defined inside the aggregate in order
to have any effect, i.e., opCast is treated like operator overloading,
for which UFCS does not work.

Maybe rename opCast to something else, say convert(), and add new opCast
methods to the structs that forward to convert()?


T

-- 
Try to keep an open mind, but not so open your brain falls out. -- theboz


More information about the Digitalmars-d-learn mailing list