Is std.typecons.Rebindable ever going to work for this?

Steven Schveighoffer schveiguy at yahoo.com
Thu Dec 17 05:57:32 PST 2009


On Thu, 17 Dec 2009 08:35:31 -0500, Michel Fortin  
<michel.fortin at michelf.com> wrote:

> I have a problem where I want to store rebindable references to other  
> objects, but I also want to ensure those objects are immutable.  
> Basically, the Rebindable template in std.typecons should do the job,  
> but it appears that it does not work with forward references...
>
> Going further, I've made this simple test case and I'm left wondering if  
> there is a way for the compiler to make that work in the future.  
> Basically, the definition of each class depends on the other class. Can  
> the compiler instantiate a template using a partially defined class? One  
> of the template has to be instantiate before the other, obviously.
>
> import std.typecons;
>
> class A {
> 	Rebindable!(const B) r;
> }
>
> class B {
> 	Rebindable!(const A) r;
> }
>
> I'm not sure if I really want to use this pattern; this is just an  
> experiment I made. But it looks quite limiting not to be able to do that.
>

Doing some tests, it appears that the issue is not strictly that you can't  
use a forward reference class as a template argument.

This appears to work:


struct S(T)
{
     T t;
}

class A {
     S!B r;
}

class B {
     S!A r;
}

Adding this to S(T) makes it not work:

struct S(T) if (is(T: Object))
{
    T t;
}

-Steve



More information about the Digitalmars-d mailing list