how is this array subtyping inside struct (bug?) possible?

Paul Backus snarwin at gmail.com
Mon Aug 10 12:03:06 UTC 2020


On Monday, 10 August 2020 at 02:50:20 UTC, mw wrote:
> On Monday, 10 August 2020 at 02:38:55 UTC, RazvanN wrote:
>> On Sunday, 9 August 2020 at 21:12:58 UTC, mw wrote:
>>
>>> I'm *directly* access the underlying array, so why its length 
>>> changed to 0 after writeln?
>>>
>> You are accessing the underlying array after it was consumed. 
>> The line writeln(s0.fns) passes class, if you want to pass the 
>> underlying array you should type `writeln(so.fns.array)` and 
>> then it will not consume the array.
>
> I know that; and that's exactly I call it a subtyping bug: i.e. 
> directly access & access via subtyping behave differently:

You are subtyping a value type (array) with a reference type 
(class). So when you directly access it, it's passed by value, 
but when you pass the subtype, it's passed by reference.

In other words, using `alias this` does not create a *true* 
subtype (according to the Liskov substitution principle), because 
it allows you to change the way the type is copied from by-value 
to by-reference.


More information about the Digitalmars-d mailing list