www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is this a bug ?

reply Basile B. <b2.temp gmx.com> writes:
This very simple stuff:

class Item
{
     alias children this;
     Item[] children;
     void populate()
     {
         children ~= new Item;
         assert(children.length == 1);
     }
}

void main()
{
     Item root = new Item;
     root.populate;
}

leads to an assertion failure. Am I too tired to see the error or 
do you think it's a bug ?
Oct 21 2016
parent ag0aep6g <anonymous example.com> writes:
On 10/21/2016 06:55 PM, Basile B. wrote:
 This very simple stuff:

 class Item
 {
     alias children this;
     Item[] children;
     void populate()
     {
         children ~= new Item;
         assert(children.length == 1);
     }
 }

 void main()
 {
     Item root = new Item;
     root.populate;
 }

 leads to an assertion failure. Am I too tired to see the error or do you
 think it's a bug ?
Bug. `alias this` is tried too eagerly. What happens: The alias this of `new Item` is evaluated, leading to `children ~= [];`.
Oct 21 2016