www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why simple code using Rebindable doesn't compile ?

reply chmike <christophe meessen.net> writes:
Hello,

here is a program stripped down to the minimum code that doesn't 
compile

----
import std.typecons;

Rebindable!(immutable TestImpl) Test;

Rebindable!(immutable TestImpl) Test;

class TestImpl
{
     void foo() {}
     Test test() { __gshared x = new immutable TestImpl; return 
rebindable(x); }
}

void main()
{
     Test t = Test.test;
}

void main()
{
     Test t = Test.test;
}
----

source/app.d(32,10): Error: circular reference to 'app.Test'
source/app.d(32,10): Error: Test is used as a type
/usr/include/dmd/phobos/std/typecons.d(1616,20): Error: template 
instance std.traits.isDynamicArray!(immutable(TestImpl)) error 
instantiating
source/app.d(27,1):        instantiated from here: 
Rebindable!(immutable(TestImpl))
/usr/include/dmd/phobos/std/typecons.d(1625,17): Error: mixin 
std.typecons.RebindableCommon!(immutable(TestImpl), TestImpl, 
Rebindable) does not match template declaration 
RebindableCommon(T, U, alias This) if (is(T == class) || is(T == 
interface) || isAssociativeArray!T)
May 30 2016
parent reply chmike <christophe meessen.net> writes:
Oops, the duplicate alias instruction and main are copy past 
error. It looks like the code was already too complex for me. ;) 
Here is the code I tested

----
import std.typecons;

Rebindable!(immutable TestImpl) Test;

class TestImpl
{
     void foo() {}
     Test test() { __gshared x = new immutable TestImpl; return 
rebindable(x); }
}

void main()
{
     Test t = Test.test;
}
----
May 30 2016
parent reply chmike <christophe meessen.net> writes:
This code compile, but array appending doesn't work

----
alias Rebindable!(immutable(InfoImpl)) Info;

class InfoImpl
{
     void foo() {}
     static immutable(InfoImpl) info()
     {
         __gshared immutable InfoImpl x = new immutable InfoImpl;
         return x;
     }
}


void main()
{
     Info t = Info.info;
     Info[] a;
     //a ~= Info.info; <--  KO Compiler Error
     a ~= rebindable(Info.info); // Ok
}
-----

Why can't info() return a Rebindable!(immutable(InfoImpl)) ?
May 30 2016
parent reply Alex Parrill <initrd.gz gmail.com> writes:
On Monday, 30 May 2016 at 10:09:19 UTC, chmike wrote:
 This code compile, but array appending doesn't work

 ----
 alias Rebindable!(immutable(InfoImpl)) Info;

 class InfoImpl
 {
     void foo() {}
     static immutable(InfoImpl) info()
     {
         __gshared immutable InfoImpl x = new immutable InfoImpl;
         return x;
     }
 }


 void main()
 {
     Info t = Info.info;
     Info[] a;
     //a ~= Info.info; <--  KO Compiler Error
     a ~= rebindable(Info.info); // Ok
 }
 -----

 Why can't info() return a Rebindable!(immutable(InfoImpl)) ?
What do you mean? `info` returns an `immutable(InfoImpl)`, not a `Rebindable!(immutable(InfoImpl))`. Rebindable doesn't apply itself to the return types of the methods of the return types (there's no reason to).
May 30 2016
parent reply chmike <christophe meessen.net> writes:
On Monday, 30 May 2016 at 21:32:46 UTC, Alex Parrill wrote:
 On Monday, 30 May 2016 at 10:09:19 UTC, chmike wrote:
 Why can't info() return a Rebindable!(immutable(InfoImpl)) ?
What do you mean? `info` returns an `immutable(InfoImpl)`, not a `Rebindable!(immutable(InfoImpl))`. Rebindable doesn't apply itself to the return types of the methods of the return types (there's no reason to).
I mean that if I change the return type of info() into Rebindable!(immutable(infoImpl)) like this Rebindable!(immutable(InfoImpl)) info() { ... return rebindable(x);} I get an error. I was explained privately that its because Rebindable... Is an lvalue and not a type. My conclusion is that rebindable is not a satisfying solution to have mutable references to immutable objects. I don't understand the rationale of these immutable references. It is too constraining.
May 30 2016
parent reply Era Scarecrow <rtcvb32 yahoo.com> writes:
On Tuesday, 31 May 2016 at 05:31:59 UTC, chmike wrote:
 My conclusion is that rebindable is not a satisfying solution 
 to have mutable references to immutable objects.

 I don't understand the rationale of these immutable references. 
 It is too constraining.
I still don't know why you're trying to use immutable. In the other thread you have listed you are trying to make a global singleton? You needed it mutable but marked immutable (for... some reason?) but all the methods won't change the contents or spirit of the object. I need to wrap my head around what you're trying to do before i can suggest anything else. Although making all members private and all functions as const would give you a mutable/unchanging object...
May 30 2016
parent chmike <christophe meessen.net> writes:
On Tuesday, 31 May 2016 at 06:40:31 UTC, Era Scarecrow wrote:
 On Tuesday, 31 May 2016 at 05:31:59 UTC, chmike wrote:
 My conclusion is that rebindable is not a satisfying solution 
 to have mutable references to immutable objects.

 I don't understand the rationale of these immutable 
 references. It is too constraining.
I still don't know why you're trying to use immutable. In the other thread you have listed you are trying to make a global singleton? You needed it mutable but marked immutable (for... some reason?) but all the methods won't change the contents or spirit of the object. I need to wrap my head around what you're trying to do before i can suggest anything else. Although making all members private and all functions as const would give you a mutable/unchanging object...
The code I gave are just examples. The reason I used immutable is because I have two types of objects. I have many Info objects (~150) which are all singletons. These Info objects have a reference to a Category objects which is also a singleton. I can have mutable singleton objects. I have a public interface that doesn't allow to modify the object. This is not the problem. The problem is that I would like that all these objects are instantiated at compile time. This is to keep the start of the program fast. There is no problem to instantiate the Category object at compile time. The problem is to instantiate the different Info objects at compile time that have a reference to the Category object. Ctfe doesn't work when a global variable is referenced. I noticed that it worked when the Category is declared immutable. I can add a reference to that Category singleton instance to the new call of the Info objects. So the goal is to create and interconnect multiple objects at compile time. Ideally I would need to store a reference to each of the Info objects in an associative array so that I can retrieve a reference to it with some key.
May 31 2016