www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Recursive Templates

reply llee <llee goucher.edu> writes:
I'm working on a set library that uses templates. The library centers around a
class named Set. This class contains an array named elems that stores the set's
elements. The definition for set is as follows:

     class Set (T) { ... T[] elems; ... }

To calculate the cartesian product of two sets, I need to be able to represent
higher order sets. I tried defining a function with the following signiture:

     static Set! (Set! (T)) product (Set! (T) a, Set! (T) b);

Unfortunately, the dmd compiler throws an error stating that It does no allow
recursive elements.

Does anyone know how to work around this problem?
Aug 09 2008
parent "Koroskin Denis" <2korden gmail.com> writes:
On Sat, 09 Aug 2008 21:40:24 +0400, llee <llee goucher.edu> wrote:

 I'm working on a set library that uses templates. The library centers =
=
 around a class named Set. This class contains an array named elems tha=
t =
 stores the set's elements. The definition for set is as follows:

      class Set (T) { ... T[] elems; ... }

 To calculate the cartesian product of two sets, I need to be able to  =
 represent higher order sets. I tried defining a function with the  =
 following signiture:

      static Set! (Set! (T)) product (Set! (T) a, Set! (T) b);

 Unfortunately, the dmd compiler throws an error stating that It does n=
o =
 allow recursive elements.

 Does anyone know how to work around this problem?
Can't confirm. The following code works: class Set (T) { T[] elems; } Set!(Set!(T)) product(T)(Set!(T) a, Set!(T) b) { Set!(Set!(T)) r =3D null; // do something return r; } void main() { auto t =3D new Set!(int); auto s =3D product(t, t); }
Aug 09 2008