opCast in class prevents destroy

bauss jj_1337 at live.dk
Tue Mar 1 09:31:46 UTC 2022


On Tuesday, 1 March 2022 at 08:16:13 UTC, Mike Parker wrote:
> On Tuesday, 1 March 2022 at 07:16:11 UTC, bauss wrote:
>
>> Right now if you want to add an additional cast then you have 
>> to implement ALL the default behaviors and then add your 
>> custom cast.
>
> It's two template functions like the OP used: one for T to 
> catch everything, and one specialization.
>
>>
>> That doesn't seem correct to me at least.
>
> Depends on your perspective I guess. For the inverse, when you 
> want to allow only one kind of cast and prevent everything 
> else, you only have to implement one template right now. If 
> that were not the case, then you'd have to implement an 
> additional catch-all template that bombs out with a static 
> assert.
>
> So either way makes sense, IMO. Though I totally understand how 
> the current behavior can be a surprise when people expect it to 
> behave like, e.g., C++.
>
> But D is not C++. So is `opCast` intended to expand the list of 
> target types (like C++), or is it intended to define it? The 
> spec says, "To define how one type can be cast to another", 
> which doesn't really answer the question.

Yes of course it's a matter of perspective.

I think the solution would be to have two functions for opCast, 
maybe something like opAdditionalCast, idk, not to break current 
behavior I guess.


More information about the Digitalmars-d-learn mailing list