toImpl deprecated, use opCast instead?

Era Scarecrow rtcvb32 at yahoo.com
Mon Jun 11 14:11:06 PDT 2012


On Monday, 11 June 2012 at 18:19:21 UTC, Manu wrote:
> On 10 June 2012 03:42, Jonathan M Davis <jmdavisProg at gmx.com> 
> wrote:
>
>> On Sunday, June 10, 2012 02:26:36 timotheecour wrote:
>>> What is the recommended approach when we have no control over 
>>> classes A or B (cf from 3rd party) to convert A to B? It 
>>> seems UFCS doesn't work in that case. Could you please 
>>> provide an example code? Thanks!
>>
>> If you want to convert between two types and you don't control 
>> the definitions of either, and neither of them have any 
>> functions which will convert from one to the (including 
>> constructors, opCast, etc.), then you're just going to have to 
>> write a function to do it. It's not gonig to work with 
>> std.conv.to or casting, but you can write such a function just 
>> like you can write any other function.
>>
>> A convertBToA(B b) {...}
>>
>> I don't think that there's anything else that you _can_ do.
>>
>
> That's very problematic for generic programming :/

  Is there any way to do implicit conversion? Or a specific call 
based on the object just being 'called'?

  As an experiment I've been trying to make a small Int struct 
that basically will fail if it hasn't been initialized, but I get 
stuck; I don't want to have to call a separate function since the 
Int will get lowered during release code to int, but if you use 
'alias value this;', then it bypasses any other checks that might 
be present when you use it as an int.

  Here's sorta my incomplete version, you should get the idea...

struct MustInitializeFirst(T)
if (isNumeric!(T)) {
   bool beenSet;
   T value;

   T opAssign(T set) {
     beenSet = true;
     return value;
   }

   //not sure what to use, so opAccess a holder for now
   T opAccess() {
     assert(beenSet);
     return value;
   }
}

alias MustInitializeFirst!int Int;


More information about the Digitalmars-d mailing list