Connecting Tech Pros Worldwide Forums | Help | Site Map

Specializing Generic Type (or Method)

abiz@onezero.org
Guest
 
Posts: n/a
#1: Jan 5 '07
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.


Joanna Carter [TeamB]
Guest
 
Posts: n/a
#2: Jan 5 '07

re: Specializing Generic Type (or Method)


<abiz@onezero.orga écrit dans le message de news:
1167980854.896758.71240@51g2000cwl.googlegroups.co m...

| 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>
{
...
}

--
Joanna Carter [TeamB]
Consultant Software Engineer


abiz@onezero.org
Guest
 
Posts: n/a
#3: Jan 5 '07

re: Specializing Generic Type (or Method)


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.

Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#4: Jan 5 '07

re: Specializing Generic Type (or Method)


Joanna Carter [TeamB] <joanna@not.for.spamwrote:

<snip>
Quote:
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, booland XCommonCase<T>
interchangably, because an X<T, boolisn'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 :)

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Closed Thread


Similar C# / C Sharp bytes