| Thread overview | |||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 29, 2008 Recursive Templates | ||||
|---|---|---|---|---|
| ||||
Is there any way to instantiate a recursive template object. For example:
class Param (T) { T values; ... }
...
Param! (Param) composite = new Param! (Param) ();
| ||||
July 29, 2008 Re: Recursive Templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to llee | "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? | |||
July 29, 2008 Re: Recursive Templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to llee | 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) {}
| |||
July 29, 2008 Re: Recursive Templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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.
| |||
July 29, 2008 Re: Recursive Templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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.
| |||
July 29, 2008 Re: Recursive Templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to llee | > Param! (Param) composite = new Param! (Param) ();
You seem to have forgotten something:
... = new Param!( Param!(MISSING) );
| |||
July 29, 2008 Re: Recursive Templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Oliver Dathe | 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.
| |||
July 29, 2008 Re: Recursive Templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to llee | 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. > | |||
July 29, 2008 Re: Recursive Templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to llee | Actually, I should probably use tuples. | |||
July 29, 2008 Re: Recursive Templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to llee Attachments:
| 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
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply