Mutually Recursive Generic Types

Discussion in 'Java' started by Marco, Jul 30, 2008.

  1. Marco

    Marco Guest

    Hi all,

    Sorry if the question has already been asked, I cannot find anything
    about it. I'd like to define a pair of classes which mutually
    recursive generics in their definition:

    class PropertyType<PV extends PropertyValue<PT extends
    PropertyType<...>>
    {
    Collection<PV> getValues ()
    void addValue ( PV value)
    }


    class PropertyValue<PT extends PropertyType<PV extends
    PropertyValue<...>>
    {
    PropertyValue ( PT type )
    PT getType()
    void setType ( PT type )
    }


    class BioProp extends PropertyType<BioValue>
    class BioValue extends PropertyValue<BioProp>


    At the moment I do that by writing PropertyType<PV extends
    PropertyValue> and alike for the value. However, this way I have
    several type safety warnings from the compiler and I wonder if there
    are suggestions on the best way to deal with such a case.

    Thanks in advance.
    Marco, Jul 30, 2008
    #1
    1. Advertising

  2. Marco

    Guest

    On Jul 30, 6:40 am, Marco <> wrote:
    > Hi all,
    >
    > Sorry if the question has already been asked, I cannot find anything
    > about it. I'd like to define a pair of classes which mutually
    > recursive generics in their definition:
    >
    > class PropertyType<PV extends PropertyValue<PT extends
    > PropertyType<...>>
    > {
    >   Collection<PV> getValues ()
    >   void addValue ( PV value)
    >
    > }
    >
    > class PropertyValue<PT extends PropertyType<PV extends
    > PropertyValue<...>>
    > {
    >   PropertyValue ( PT type )
    >   PT getType()
    >   void setType ( PT type )
    >
    > }
    >
    > class BioProp extends PropertyType<BioValue>
    > class BioValue extends PropertyValue<BioProp>
    >
    > Thanks in advance.


    This seems to do the trick.

    class PropertyType<T extends PropertyType<T, V>, V extends
    PropertyValue<V, T>>
    {
    Collection<V> values;
    }

    class PropertyValue<V extends PropertyValue<V, T>, T extends
    PropertyType<T, V>>
    {
    T type;
    }

    class BioProp extends PropertyType<BioProp, BioValue>
    {
    }

    class BioValue extends PropertyValue<BioValue, BioProp>
    {
    }

    -Lucas
    , Jul 30, 2008
    #2
    1. Advertising

  3. Marco

    Daniel Pitts Guest

    Marco wrote:
    > Hi all,
    >
    > Sorry if the question has already been asked, I cannot find anything
    > about it. I'd like to define a pair of classes which mutually
    > recursive generics in their definition:
    >
    > class PropertyType<PV extends PropertyValue<PT extends
    > PropertyType<...>>
    > {
    > Collection<PV> getValues ()
    > void addValue ( PV value)
    > }
    >
    >
    > class PropertyValue<PT extends PropertyType<PV extends
    > PropertyValue<...>>
    > {
    > PropertyValue ( PT type )
    > PT getType()
    > void setType ( PT type )
    > }
    >
    >
    > class BioProp extends PropertyType<BioValue>
    > class BioValue extends PropertyValue<BioProp>
    >
    >
    > At the moment I do that by writing PropertyType<PV extends
    > PropertyValue> and alike for the value. However, this way I have
    > several type safety warnings from the compiler and I wonder if there
    > are suggestions on the best way to deal with such a case.
    >
    > Thanks in advance.
    >


    The problem is in the "..." that has the code that you've omitted.

    You need something like this:

    class PropertyType<T extends PropertyType<T, V>, V extends
    PropertyValue<V, T>> {}

    class PropertyValue<V extends PropertyValue<V, T>, T extends
    PropertyType<T, V>> {}


    class BioProp extends PropertyType<BioProp, BioValue>{}
    class BioValue extends PropertyValue<BioValue,BioProp>{}



    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Jul 31, 2008
    #3
  4. Marco

    Marco Guest

    On 30 Jul, 13:42, wrote:

    > This seems to do the trick.
    >
    > class PropertyType<T extends PropertyType<T, V>, V extends
    > PropertyValue<V, T>>


    Thank you all for replying. Two generics works, although, from the
    conceptual point of view, it sounds weird to me. I mean: I never need
    T inside PropertyType and dually I never need V inside PropertyValue.
    Actually T and V are always equal to the same class being declared.
    Marco, Jul 31, 2008
    #4
  5. Marco

    Guest

    On Jul 31, 7:10 am, Marco <> wrote:
    > On 30 Jul, 13:42, wrote:
    >
    > > This seems to do the trick.

    >
    > >    class PropertyType<T extends PropertyType<T, V>, V extends
    > > PropertyValue<V, T>>

    >
    > Thank you all for replying. Two generics works, although, from the
    > conceptual point of view, it sounds weird to me. I mean: I never need
    > T inside PropertyType and dually I never need V inside PropertyValue.
    > Actually T and V are always equal to the same class being declared.


    Ok. Try this. It at least compiles, but I'm not sure how easy it would
    be to work with in general.

    class PropertyType<V extends PropertyValue<? extends
    PropertyType<V>>>
    {
    Collection<V> values;
    }

    class PropertyValue<T extends PropertyType<? extends
    PropertyValue<T>>>
    {
    T type;
    }

    class BioProp extends PropertyType<BioValue> {}
    class BioValue extends PropertyValue<BioProp> {}


    -Lucas
    , Aug 1, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric

    Re: Mutually referential types

    Eric, Aug 15, 2003, in forum: C Programming
    Replies:
    5
    Views:
    344
    Mark Haigh
    Aug 17, 2003
  2. Chris Torek

    Re: Mutually referential types

    Chris Torek, Aug 15, 2003, in forum: C Programming
    Replies:
    1
    Views:
    295
    Colin JN Breame
    Aug 16, 2003
  3. Niklas Matthies
    Replies:
    0
    Views:
    822
    Niklas Matthies
    Oct 24, 2006
  4. jeniffer

    Example of 2 mutually recursive functions

    jeniffer, Apr 13, 2006, in forum: C Programming
    Replies:
    6
    Views:
    313
  5. Revence Kalibwani

    Mutually-Recursive Functions

    Revence Kalibwani, Jun 7, 2007, in forum: Ruby
    Replies:
    13
    Views:
    191
    bbiker
    Jun 7, 2007
Loading...

Share This Page