www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using RefCounted in recursive structures and templates

reply Lodovico Giaretta <lodovico giaretart.net> writes:
Hi,

I'm trying to use std.typecons.RefCounted on recursive structures 
to emulate garbage-collected classes, but it doesn't work, 
because the RefCounted implementation ends up creating a cycle in 
the structure hierarchy.

import std.typecons: RefCounted;

struct S
{
     RefCounted!S s; // error: struct S no size yet for forward 
reference
}

Even worst when I try to mix this with templates:

alias Node(T) = RefCounted!(_Node!T);
struct _Node(T)
{
     Node!T parent; // error: recursive template expansion
}

Is there some way to bypass these limitations, and use refcounted 
structures like classes?

Thank you in advance.
May 10 2016
next sibling parent reply ag0aep6g <anonymous example.com> writes:
Am 10.05.2016 um 21:43 schrieb Lodovico Giaretta:
 import std.typecons: RefCounted;

 struct S
 {
      RefCounted!S s; // error: struct S no size yet for forward reference
 }
This used to work with 2.067. I've filed a phobos regression for this very code, and two related dmd issues with different reductions: https://issues.dlang.org/show_bug.cgi?id=16011 - [REG2.068] recursive RefCounted used to work https://issues.dlang.org/show_bug.cgi?id=16012 - [REG2.070] forward reference with alias this https://issues.dlang.org/show_bug.cgi?id=16013 - [REG2.072a] ICE with mutually dependent structs and alias this
May 10 2016
parent Lodovico Giaretta <lodovico giaretart.net> writes:
On Tuesday, 10 May 2016 at 22:10:06 UTC, ag0aep6g wrote:
 Am 10.05.2016 um 21:43 schrieb Lodovico Giaretta:
 import std.typecons: RefCounted;

 struct S
 {
      RefCounted!S s; // error: struct S no size yet for 
 forward reference
 }
This used to work with 2.067. I've filed a phobos regression for this very code, and two related dmd issues with different reductions: https://issues.dlang.org/show_bug.cgi?id=16011 - [REG2.068] recursive RefCounted used to work https://issues.dlang.org/show_bug.cgi?id=16012 - [REG2.070] forward reference with alias this https://issues.dlang.org/show_bug.cgi?id=16013 - [REG2.072a] ICE with mutually dependent structs and alias this
Thank you very much.
May 11 2016
prev sibling parent ag0aep6g <anonymous example.com> writes:
Am 10.05.2016 um 21:43 schrieb Lodovico Giaretta:
 alias Node(T) = RefCounted!(_Node!T);
 struct _Node(T)
 {
      Node!T parent; // error: recursive template expansion
 }
I think this is expected. Can't have cycles like that in template instantiations.
May 10 2016