July 29, 2008 Re: Recursive Templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to llee | "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; | |||
July 29, 2008 Re: Recursive Templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to llee | "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
| |||
July 29, 2008 Re: Recursive Templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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
| |||
July 30, 2008 Re: Recursive Templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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;
// ...
}
| |||
July 30, 2008 Re: Recursive Templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Koroskin Denis | "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". | |||
July 30, 2008 Re: Recursive Templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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;
>> // ...
>> }
> I'm not entirely sure what you're asking.
>
he wants is(typeof(this) == typeof(this.elems[0]));
It will be a kind of tree.
| |||
July 30, 2008 Re: Recursive Templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to BCS | "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; >>> // ... >>> } >> I'm not entirely sure what you're asking. >> > > he wants is(typeof(this) == typeof(this.elems[0])); > > It will be a kind of tree. Ahh. | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply