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

Steven Schveighoffer schveiguy at
Mon Aug 10 20:00:00 UTC 2020

On 8/10/20 3:49 PM, mw wrote:
> Ideally, to the user, subtyping should just perform simple forward to 
> the underlying data member, i.e.
> writeln(s0.fns);
> should be translated by the *compiler* to:
> writeln(s0.fns.array);
> Obviously it failed to do so here.

It would never do that. Because classes are based on Object, and writeln 
is defined to accept an Object and print it that way (using 
Object.toString). So even if the range methods weren't available, it 
wouldn't instead pass the base type.

What's happening is that inadvertently, you have created a range type. 
writeln is going to PREFER using range mechanisms over Object.toString, 
so when it sees it can use range primitives, it uses those. This is the 
danger of duck-typing.

What you want is array functionality, not range functionality on the 
class. So you need to avoid those mechanisms. Try the disable idea, see 
if it works.


More information about the Digitalmars-d mailing list