digitalmars.D - Recursive Templates
- llee (4/4) Jul 29 2008 Is there any way to instantiate a recursive template object. For example...
- Jarrett Billingsley (3/7) Jul 29 2008 What are you trying to do? Why didn't you post this in d.learn?
- llee (4/4) Jul 29 2008 I'm trying to define an object named Param that has a member named value...
- Steven Schveighoffer (23/29) Jul 29 2008 enum valuetype
- bearophile (6/13) Jul 29 2008 That's an interesting solution, I'll probably find a way to use it mysel...
- llee (5/5) Jul 29 2008 I ran into a similar problem when working with sets. I could define sets...
- BCS (3/13) Jul 29 2008 that might be a good thing
- Jarrett Billingsley (5/12) Jul 29 2008 A set of sets of ... what? You just need to specify what type it is. "...
- Koroskin Denis (8/24) Jul 30 2008 How about, say, Set of Set of the self type? So that it becomes this:
- Jarrett Billingsley (18/24) Jul 30 2008 I'm not entirely sure what you're asking.
- BCS (3/15) Jul 30 2008 he wants is(typeof(this) == typeof(this.elems[0]));
- Jarrett Billingsley (3/18) Jul 30 2008 Ahh.
- BCS (8/17) Jul 29 2008 No, there is no reason that you can't work with such a type but the synt...
- Oliver Dathe (2/3) Jul 29 2008 You seem to have forgotten something:
- llee (2/7) Jul 29 2008 That's what I need to know. I don't know what to put there to allow comp...
- Bill Baxter (10/18) Jul 29 2008 Since the thing you end up with is a template still, you have to define ...
- llee (1/1) Jul 29 2008 Actually, I should probably use tuples.
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
"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
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
"llee" wroteI'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
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
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
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_paradoxSet! (Set) family = ... does not work.
Jul 29 2008
"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
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...How about, say, Set of Set of the self type? So that it becomes this: class MySet { MySet[] elems; // ... }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 30 2008
"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
Reply to Jarrett,"Koroskin Denis" <2korden gmail.com> wrote in message news:op.ue3up2utenyajd proton.creatstudio.intranet...he wants is(typeof(this) == typeof(this.elems[0])); It will be a kind of tree.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.
Jul 30 2008
"BCS" <ao pathlink.com> wrote in message news:55391cb32fbe48cac041c4d5a41e news.digitalmars.com...Reply to Jarrett,Ahh."Koroskin Denis" <2korden gmail.com> wrote in message news:op.ue3up2utenyajd proton.creatstudio.intranet...he wants is(typeof(this) == typeof(this.elems[0])); It will be a kind of tree.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.
Jul 30 2008
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
Param! (Param) composite = new Param! (Param) ();You seem to have forgotten something: ... = new Param!( Param!(MISSING) );
Jul 29 2008
Oliver Dathe Wrote: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.Param! (Param) composite = new Param! (Param) ();You seem to have forgotten something: ... = new Param!( Param!(MISSING) );
Jul 29 2008
Content-Disposition: inline On Wed, Jul 30, 2008 at 4:36 AM, llee <llee goucher.edu> wrote:Oliver Dathe Wrote: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? --bbThat's what I need to know. I don't know what to put there to allow composite to be an array of arbitrary Params.Param! (Param) composite = new Param! (Param) ();You seem to have forgotten something: ... = new Param!( Param!(MISSING) );
Jul 29 2008