www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Recursive Templates

reply llee <llee goucher.edu> writes:
Is there any way to instantiate a recursive template object. For example:

class Param (T) { T values; ... }

...

Param! (Param) composite = new Param! (Param) ();
Jul 29 2008
next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"llee" <llee goucher.edu> wrote in message 
news:g6nmer$23ke$1 digitalmars.com...
 Is there any way to instantiate a recursive template object. For example:

 class Param (T) { T values; ... }

 ...

 Param! (Param) composite = new Param! (Param) ();

What are you trying to do? Why didn't you post this in d.learn?
Jul 29 2008
next sibling parent reply llee <llee goucher.edu> writes:
I'm trying to define an object named Param that has a member named value. Value
can be either a variable of type T, and array of type T[], or an array of other
Params.
Using templates, I could implement the first two cases:
     class Param (T) { T value; ... }
but I could not handle the recursive case.
Jul 29 2008
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"llee" wrote
 I'm trying to define an object named Param that has a member named value. 
 Value can be either a variable of type T, and array of type T[], or an 
 array of other Params.
 Using templates, I could implement the first two cases:
     class Param (T) { T value; ... }
 but I could not handle the recursive case.

enum valuetype { T, Array, Param } class Param(T, valuetype vt = valuetype.T) { static if(vt == valuetype.T) { T value; } else if(vt == valuetype.Array) { T[] value; } else if(vt == valuetype.Param) { Param!(T, vt) value; } } -Steve
Jul 29 2008
parent bearophile <bearophileHUGS lycos.com> writes:
Steven Schveighoffer:
 enum valuetype
 {
    T,
    Array,
    Param
 }
 ...

That's an interesting solution, I'll probably find a way to use it myself. It follows the old advice: when the type system of you language isn't powerful enough for your purposes, punch a hole in it an go on ;-) As more and more "advanced" features are added to D 2.x, its type system may show some limitations (on the other hand in most situations I don't need a bulletproof type system like Haskell one). Bye, bearophile
Jul 29 2008
prev sibling next sibling parent reply llee <llee goucher.edu> writes:
I ran into a similar problem when working with sets. I could define sets using
templates, but I couldn't define sets of sets without the compiler complaining
about recursive definitions.

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

but how would I define a set of sets?.

     Set! (Set) family = ... does not work.
Jul 29 2008
next sibling parent BCS <ao pathlink.com> writes:
Reply to llee,

 I ran into a similar problem when working with sets. I could define
 sets using templates, but I couldn't define sets of sets without the
 compiler complaining about recursive definitions.
 
 For example:
 class Set (T) { T[] elems; ... }
 but how would I define a set of sets?.

that might be a good thing sets of sets = strange things: http://en.wikipedia.org/wiki/Russell's_paradox
 
 Set! (Set) family = ... does not work.
 

Jul 29 2008
prev sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"llee" <llee goucher.edu> wrote in message 
news:g6nq50$2b9n$1 digitalmars.com...
I ran into a similar problem when working with sets. I could define sets 
using templates, but I couldn't define sets of sets without the compiler 
complaining about recursive definitions.

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

 but how would I define a set of sets?.

     Set! (Set) family = ... does not work.

A set of sets of ... what? You just need to specify what type it is. "Set" is not a type, it is a template. Set!(Set!(int)) family;
Jul 29 2008
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Koroskin Denis" <2korden gmail.com> wrote in message 
news:op.ue3up2utenyajd proton.creatstudio.intranet...
 How about, say, Set of Set of the self type? So that it becomes this:

 class MySet
 {
 MySet[] elems;
 // ...
 }

I'm not entirely sure what you're asking. class Set(T) { T[] elems; this() { Stdout.formatln("set"); } } class Set(T : Set!(U), U) { Set!(U) elems; this() { Stdout.formatln("set of sets"); } } void main() { auto x = new Set!(Set!(int)); } prints "set of sets".
Jul 30 2008
parent reply BCS <ao pathlink.com> writes:
Reply to Jarrett,

 "Koroskin Denis" <2korden gmail.com> wrote in message
 news:op.ue3up2utenyajd proton.creatstudio.intranet...
 
 How about, say, Set of Set of the self type? So that it becomes this:
 
 class MySet
 {
 MySet[] elems;
 // ...
 }


he wants is(typeof(this) == typeof(this.elems[0])); It will be a kind of tree.
Jul 30 2008
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"BCS" <ao pathlink.com> wrote in message 
news:55391cb32fbe48cac041c4d5a41e news.digitalmars.com...
 Reply to Jarrett,

 "Koroskin Denis" <2korden gmail.com> wrote in message
 news:op.ue3up2utenyajd proton.creatstudio.intranet...

 How about, say, Set of Set of the self type? So that it becomes this:

 class MySet
 {
 MySet[] elems;
 // ...
 }


he wants is(typeof(this) == typeof(this.elems[0])); It will be a kind of tree.

Ahh.
Jul 30 2008
prev sibling parent "Koroskin Denis" <2korden gmail.com> writes:
On Tue, 29 Jul 2008 23:59:12 +0400, Jarrett Billingsley  
<kb3ctd2 yahoo.com> wrote:

 "llee" <llee goucher.edu> wrote in message
 news:g6nq50$2b9n$1 digitalmars.com...
 I ran into a similar problem when working with sets. I could define sets
 using templates, but I couldn't define sets of sets without the compiler
 complaining about recursive definitions.

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

 but how would I define a set of sets?.

     Set! (Set) family = ... does not work.

A set of sets of ... what? You just need to specify what type it is. "Set" is not a type, it is a template. Set!(Set!(int)) family;

How about, say, Set of Set of the self type? So that it becomes this: class MySet { MySet[] elems; // ... }
Jul 30 2008
prev sibling next sibling parent BCS <ao pathlink.com> writes:
Reply to llee,

 Is there any way to instantiate a recursive template object. For
 example:
 
 class Param (T) { T values; ... }
 
 ...
 
 Param! (Param) composite = new Param! (Param) ();
 

No, there is no reason that you can't work with such a type but the syntax and type system internals make it impossible in practice. You might be able to work around this with some combination of alias's templates tart resolve to types and other hacks. The simplest option might be something like this: class Param (T) { ... } class P2 : Param!(P2) {}
Jul 29 2008
prev sibling next sibling parent reply Oliver Dathe <o.dathe gmx.de> writes:
 Param! (Param) composite = new Param! (Param) ();

You seem to have forgotten something: ... = new Param!( Param!(MISSING) );
Jul 29 2008
next sibling parent llee <llee goucher.edu> writes:
Oliver Dathe Wrote:

 Param! (Param) composite = new Param! (Param) ();

You seem to have forgotten something: ... = new Param!( Param!(MISSING) );

That's what I need to know. I don't know what to put there to allow composite to be an array of arbitrary Params.
Jul 29 2008
prev sibling parent "Bill Baxter" <wbaxter gmail.com> writes:
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On Wed, Jul 30, 2008 at 4:36 AM, llee <llee goucher.edu> wrote:

 Oliver Dathe Wrote:

 Param! (Param) composite = new Param! (Param) ();

You seem to have forgotten something: ... = new Param!( Param!(MISSING) );

That's what I need to know. I don't know what to put there to allow composite to be an array of arbitrary Params.

Since the thing you end up with is a template still, you have to define a new template that defines the alias you want: template SetOfSets(T) { alias Set!(Set!(T)) SetOfSets; } This is one place where D could be nicer. I think extending the template shortcut syntax to aliases would be nice: alias(T) Set!(Set!(T)) SetOfSets!(T); It works for classes, structs and functions -- why not aliases too? --bb
Jul 29 2008
prev sibling parent llee <llee goucher.edu> writes:
Actually, I should probably use tuples.
Jul 29 2008