writeln, alias this and dynamic arrays.

matthewh matthew.hanham at gmail.com
Fri Nov 17 02:13:54 UTC 2017


I am new to D and have been fiddling with bits and pieces.
I have this code:

import std.stdio : writeln;
import std.format : format;

class Base
{
     override
     {
         //string toString()
         //{
          //   return format("%s", store);
         //}
     }
     ubyte[] store;
     alias store this;
     this()
     {
         store = [1, 2, 3, 4, 5, 6, 7, 8];
     }
}

class Top
{
     Base store;
     alias store this;
     this()
     {
         store = new Base;
     }
}

void main()
{
     auto f = new Top;
     writeln(f.store);
     writeln(f.store);
}

as is it produces:

[1, 2, 3, 4, 5, 6, 7, 8]
[]

I expected it to produce:

[1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8]

And with the toString override included it does.

Why does the version without the toString override output an 
empty array?

Thank you.


More information about the Digitalmars-d-learn mailing list