Mutually Recursive Generic Types

M

Marco

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.
 
L

lscharen

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
 
D

Daniel Pitts

Marco said:
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>{}
 
M

Marco

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.
 
L

lscharen

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top