www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Issue with std.typecons.scoped and Interfaces

--001636ed6ca8a4d01604aa9925b2
Content-Type: text/plain; charset=ISO-8859-1

Sorry this is long, but it's a somewhat complicated issue that I think
someone who knows a lot about is() could solve very quickly. I hit this a
while back but didn't figure out exactly what the issue was until today. It
seems that std.typecons.scoped doesn't play nice with interfaces:

scopedtest.d (shortened somewhat):
import std.typecons, std.stdio;

class A {
this() { writeln("A"); }
~this() { writeln("~A"); }
}

interface Bob {}

class ABob : A, Bob {
this() { writeln("ABob"); }
~this() { writeln("~ABob"); }
}

void main() { auto abob = scoped!ABob(); }


compiler output:
$ gdc -o scopedtest scopedtest.d
/usr/include/d2/4.6.0/std/typecons.d:2571: Error: template
std.typecons.destroy(T) if (is(T == class)) does not match any function
template declaration
/usr/include/d2/4.6.0/std/typecons.d:2571: Error: template
std.typecons.destroy(T) if (is(T == class)) cannot deduce template function
from argument types !()(A,Bob)
/usr/include/d2/4.6.0/std/typecons.d:2530: Error: template instance
std.typecons.destroy!(ABob) error instantiating
scopedtest.d:18:        instantiated from here: scoped!(ABob,)
scopedtest.d:18: Error: template instance std.typecons.scoped!(ABob,) error
instantiating


std.typecons.destroy:
/*
  Used by scoped() above.  Calls the destructors of an object
  transitively up the inheritance path, but work properly only if the
  static type of the object (T) is known.
 */
private void destroy(T)(T obj) if (is(T == class))
{
    static if (is(typeof(obj.__dtor())))
    {
        obj.__dtor();
    }
    static if (!is(T == Object) && is(T Base == super))
    {
        Base b = obj;
        destroy(b); // <-- this instantiation is failing
    }
}


So it looks like instead of a single type, we're getting a tuple of some
sort because ABob has multiple "superclasses" ? I haven't played with tuples
enough to know exactly what's going on here.

--001636ed6ca8a4d01604aa9925b2
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div>Sorry this is long, but it&#39;s a somewhat complicated issue that I t=
hink someone who knows a lot about is() could solve very quickly.=A0I hit t=
his a while back but didn&#39;t figure out exactly what the issue was until=
 today. It seems that std.typecons.scoped doesn&#39;t play nice with interf=
aces:</div>

<div><br></div><div>scopedtest.d (shortened somewhat):</div><div><div>impor=
t std.typecons,=A0std.stdio;</div><div><br></div><div>class A {</div><div>t=
his() { writeln(&quot;A&quot;); }</div><div>~this() { writeln(&quot;~A&quot=
;); }</div>

<div>}</div><div><br></div><div>interface Bob {}</div><div><br></div><div>c=
lass ABob : A, Bob {</div><div>this() { writeln(&quot;ABob&quot;); }</div><=
div>~this() { writeln(&quot;~ABob&quot;); }</div><div>}</div><div><br>
</div>
<div>void main() {=A0auto abob =3D scoped!ABob();=A0}</div></div><div><br><=
/div><div><br></div><div>compiler output:</div><div><div>$ gdc -o scopedtes=
t scopedtest.d=A0</div><div>/usr/include/d2/4.6.0/std/typecons.d:2571: Erro=
r: template std.typecons.destroy(T) if (is(T =3D=3D class)) does not match =
any function template declaration</div>

<div>/usr/include/d2/4.6.0/std/typecons.d:2571: Error: template std.typecon=
s.destroy(T) if (is(T =3D=3D class)) cannot deduce template function from a=
rgument types !()(A,Bob)</div><div>/usr/include/d2/4.6.0/std/typecons.d:253=
0: Error: template instance std.typecons.destroy!(ABob) error instantiating=
</div>

<div>scopedtest.d:18: =A0 =A0 =A0 =A0instantiated from here: scoped!(ABob,)=
</div><div>scopedtest.d:18: Error: template instance std.typecons.scoped!(A=
Bob,) error instantiating</div><div><br></div><div><br></div><div>std.typec=
ons.destroy:</div>

<div><div>/*</div><div>=A0 Used by scoped() above. =A0Calls the destructors=
 of an object</div><div>=A0 transitively up the inheritance path, but work =
properly only if the</div><div>=A0 static type of the object (T) is known.<=
/div>

<div>=A0*/</div><div>private void destroy(T)(T obj) if (is(T =3D=3D class))=
</div><div>{</div><div>=A0 =A0 static if (is(typeof(obj.__dtor())))</div><d=
iv>=A0 =A0 {</div><div>=A0 =A0 =A0 =A0 obj.__dtor();</div><div>=A0 =A0 }</d=
iv><div>=A0 =A0 static if (!is(T =3D=3D Object) &amp;&amp; is(T Base =3D=3D=
 super))</div>

<div>=A0 =A0 {</div><div>=A0 =A0 =A0 =A0 Base b =3D obj;</div><div>=A0 =A0 =
=A0 =A0 destroy(b); // &lt;-- this instantiation is failing</div><div>=A0 =
=A0 }</div><div>}</div></div><div><br></div><div><br></div></div><div>So it=
 looks like instead of a single type, we&#39;re getting a tuple of some sor=
t because ABob has multiple &quot;superclasses&quot; ? I haven&#39;t played=
 with tuples enough to know exactly what&#39;s going on here.</div>


--001636ed6ca8a4d01604aa9925b2--
Aug 15 2011