Specializing Generic Type (or Method)

  • Thread starter Thread starter abiz
  • Start date Start date
A

abiz

What is the current thinking about specializing generic C# types?

I've come across a situation where I'd like to do something like this:

struct X<T,U>
{
...
}

struct XCommonCase<T> = X<T,bool>

Even if this example used classes and not structs, inheritance wouldn't
quite be the right thing here. The only way I know of to get this
specialization is to clone X in XCommonCase. It'd be nice if there
were something better.

It might also be useful to have similar specialization with generic
methods.
 
<[email protected]> a écrit dans le message de (e-mail address removed)...

| What is the current thinking about specializing generic C# types?
|
| I've come across a situation where I'd like to do something like this:
|
| struct X<T,U>
| {
| ...
| }
|
| struct XCommonCase<T> = X<T,bool>
|
| Even if this example used classes and not structs, inheritance wouldn't
| quite be the right thing here. The only way I know of to get this
| specialization is to clone X in XCommonCase. It'd be nice if there
| were something better.
|
| It might also be useful to have similar specialization with generic
| methods.

What is wrong with doing what you suggest ? As long as you use a class
instead of a struct and get the syntax right, this is perfectly permissible.

class X<T, U>
{
...
}

class XCommonCase<T> : X<T, bool>
{
...
}
 
What is wrong with doing what you suggest ? As long as you use a class
instead of a struct and get the syntax right, this is perfectly permissible.

class X<T, U>
{
...
}

class XCommonCase<T> : X<T, bool>
{
...
}

The trouble is you then can't use X<T, bool> and XCommonCase<T>
interchangably, because an X<T, bool> isn't necessarily an
XCommonCase<T>. Personally I really dislike using inheritance as a form
of typedef, which is what the above is, effectively. I have a prejudice
against inheritance in general though :)
 
Joanna,

First of all, I am using a struct. In my application it is not
appropriate to use a class here.

Second, I do not think that inheritance is not the right relation
between the specialized type and the more general type. I cannot
explain exactly why. But one reason is that it does not make sense to
me that a class inheritance hierarchy would have to be introduced
simply in order to have a shorthand notation for the specialized
generic class XCommonCase. Generic specialization seems to me to be a
different kind of specialization than class specialization. Another
piece of evidence for this is that generic specialization makes sense
with structs.
 

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

Back
Top