steve bull said:
it seems to me that the implementation of generics allows nothing but
the most trivial of exercises to be done - without a means of doing
anything different based on the type allows only duplication and
little variation.
That's not true at all. It doesn't allow the kind of templating C++
does, but frankly I'm glad of that.
For example if I want to write
public class SwatchPanel<TSwatch> : System.Windows.Forms.Panel where
TSwatch : Swatch
{
List<TSwatch> swatchList;
public MethodA
{
Swatch.Write(swatchList);
}
}
to have different methods for writing out the swatches based on type.
the compiler does not appear to let me. I cannot say TSwatch.Write
and when I write the following under the Swatch class
<snip>
Indeed. And that would be a bad way of writing it, IMO. It looks like
you want polymorphism at that stage - either have a class which
encapsulates a list of Swatches (generically) and override the
behaviour appropriately, or have one Write method which uses
polymorphism on the Swatch class to work out how to write each entry.
Alternatively, have an instance method in SwatchPanel which writes its
list out.
Maybe I am doing something very stupid here but it appears to be very
difficult to do anything of any use with generics except cookie
cutting. They are a brain dead implementation.
Cookie-cutting is a good example of exactly what generics are for. The
way you're trying to do things isn't how they're intended to be used,
but that doesn't mean generics are bad. What you're trying to do breaks
object orientation in various ways - for instance, by having a set of
static methods in the base class, you're including knowledge of each of
the subclasses into Swatch into the that base class, when that
knowledge should either be in the subclasses themselves, or at worst
the specialised subclasses of SwatchPanel.
If I make SwatchPanel a normal class and derive classes from it I can
do everything all of the above w/o problem. Then the problem is with
making the Swatch.Write work with List<Swatch> swatchList. Each child
class then has to convert from List<Swatch> to List<ColorSwatch> etc
when it wants to do the actual write in the method appropriate to it.
I am sorry if this is not the place to bring this up. But the
implementation seems pretty poorly thought out.
There are limitations which are genuinely problematic, but I don't
think I'd agree that anything you've brought up so far is actually a
problem with generics.