Re: writeln, alias this and dynamic arrays.

2017-11-22 Thread matthewh via Digitalmars-d-learn

Thank you all for the helpful responses.
I will read more about ranges.


writeln, alias this and dynamic arrays.

2017-11-16 Thread matthewh via Digitalmars-d-learn

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.