473,402 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Feature request for C# Generic inheritence

Hello,

What do you think about the following features:

public class GenericDecorator<T: T
{

}

can leverage to a few things:

public interface IChannel
{
void Connect();
void Disconnect();
}

public class TcpChannel : IChannel
{
....
}

public class ChannelDescriber<T: T where T : IChannel, class
{
public string ChannelName {get; set;}
}

another more complicated example:

public class Extender<T,K: T
{
public K Data {get;set;}
}

which can be use to :
Extender<EventArgs,stringextender;

extender.Data (give the same ability as EventArgs<Tbut more generic which can leverage other types that do not support generics.

Last is for delegates aka

delegate void extendDelegate<T,K>(K t) : T where T : delegate
// K should be added as first parameter.
and usage:

extendDelegate<ThreadStart,stringStart;

Start("hello world");

which means that even further we can do this:

System.Threading.Thread.Start(Start("ido",null)); // anonymous delegates feature

void Start(string name, object state);

you can vote for the feature in the following like:
https://connect.microsoft.com/Visual...&wa=wsignin1.0
--
Best,

Ido Samuelson
Sep 26 '07 #1
7 1624
The trouble is that .NET generics, unlike C++ templates, are completely
compiled in generic form. However, all base classes must be known at
compile time. Only C++ templates, which are made concrete before being
compiled, can inherit from template parameters. However .NET classes can
use the curiously recurring template pattern which has some overlapping
applications.

"Ido Samuelson" <is********@nana.co.ilwrote in message
news:4C**********************************@microsof t.com...
Hello,

What do you think about the following features:

public class GenericDecorator<T: T
{

}

can leverage to a few things:

public interface IChannel
{
void Connect();
void Disconnect();
}

public class TcpChannel : IChannel
{
....
}

public class ChannelDescriber<T: T where T : IChannel, class
{
public string ChannelName {get; set;}
}

another more complicated example:

public class Extender<T,K: T
{
public K Data {get;set;}
}

which can be use to :
Extender<EventArgs,stringextender;

extender.Data (give the same ability as EventArgs<Tbut more generic which
can leverage other types that do not support generics.

Last is for delegates aka

delegate void extendDelegate<T,K>(K t) : T where T : delegate
// K should be added as first parameter.
and usage:

extendDelegate<ThreadStart,stringStart;

Start("hello world");

which means that even further we can do this:

System.Threading.Thread.Start(Start("ido",null)); // anonymous delegates
feature

void Start(string name, object state);

you can vote for the feature in the following like:
https://connect.microsoft.com/Visual...&wa=wsignin1.0
--
Best,

Ido Samuelson
Sep 26 '07 #2
This is correct. I thought that for generic inheritance, the compiler will
create an explicit type at compile time. Same way anonymous method works.

"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:#o**************@TK2MSFTNGP02.phx.gbl...
The trouble is that .NET generics, unlike C++ templates, are completely
compiled in generic form. However, all base classes must be known at
compile time. Only C++ templates, which are made concrete before being
compiled, can inherit from template parameters. However .NET classes can
use the curiously recurring template pattern which has some overlapping
applications.

"Ido Samuelson" <is********@nana.co.ilwrote in message
news:4C**********************************@microsof t.com...
Hello,

What do you think about the following features:

public class GenericDecorator<T: T
{

}

can leverage to a few things:

public interface IChannel
{
void Connect();
void Disconnect();
}

public class TcpChannel : IChannel
{
...
}

public class ChannelDescriber<T: T where T : IChannel, class
{
public string ChannelName {get; set;}
}

another more complicated example:

public class Extender<T,K: T
{
public K Data {get;set;}
}

which can be use to :
Extender<EventArgs,stringextender;

extender.Data (give the same ability as EventArgs<Tbut more generic
which can leverage other types that do not support generics.

Last is for delegates aka

delegate void extendDelegate<T,K>(K t) : T where T : delegate
// K should be added as first parameter.
and usage:

extendDelegate<ThreadStart,stringStart;

Start("hello world");

which means that even further we can do this:

System.Threading.Thread.Start(Start("ido",null)); // anonymous delegates
feature

void Start(string name, object state);

you can vote for the feature in the following like:
https://connect.microsoft.com/Visual...&wa=wsignin1.0
--
Best,

Ido Samuelson
Sep 27 '07 #3
Ido Samuelson <id***********@gmail.comwrote:
I know how the compiler works for the current generics. I was talking about
how it "will" work for the feature request. So for generic inheritance an
explicit code generated type will be created at compile time. This will
resolve the v-table issue because the base class will be known exactly.

so for your example

class Gen<T: T where T : IList
{
int IList.Count { get { ... } }
}

at runtime a new type will be created:

Class GenXXXList : List
{
int IList.Count { get {... } }
}

and all the code refered to Gen<Twill be replaced with GenXXXList. (same
way as anonymous methods)
So a Gen<Listused in one assembly wouldn't the same type as a
Gen<Listin another assembly? Sounds like a recipe for hard-to-
diagnose bugs.

--
Jon Skeet - <sk***@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
Sep 28 '07 #4
isn't that the same case with anonymous methods or extension methods? in
some cases both can cause unexpected behaviors.
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Ido Samuelson <id***********@gmail.comwrote:
>I know how the compiler works for the current generics. I was talking
about
how it "will" work for the feature request. So for generic inheritance an
explicit code generated type will be created at compile time. This will
resolve the v-table issue because the base class will be known exactly.

so for your example

class Gen<T: T where T : IList
{
int IList.Count { get { ... } }
}

at runtime a new type will be created:

Class GenXXXList : List
{
int IList.Count { get {... } }
}

and all the code refered to Gen<Twill be replaced with GenXXXList.
(same
way as anonymous methods)

So a Gen<Listused in one assembly wouldn't the same type as a
Gen<Listin another assembly? Sounds like a recipe for hard-to-
diagnose bugs.

--
Jon Skeet - <sk***@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
Sep 28 '07 #5
Further, it is never a good idea to have the same class name, same type and
same namespace in different assemblies. So I really don't see a problem in
debugging.

"Ido Samuelson" <id***********@gmail.comwrote in message
news:ea**************@TK2MSFTNGP06.phx.gbl...
isn't that the same case with anonymous methods or extension methods? in
some cases both can cause unexpected behaviors.
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
>Ido Samuelson <id***********@gmail.comwrote:
>>I know how the compiler works for the current generics. I was talking
about
how it "will" work for the feature request. So for generic inheritance
an
explicit code generated type will be created at compile time. This will
resolve the v-table issue because the base class will be known exactly.

so for your example

class Gen<T: T where T : IList
{
int IList.Count { get { ... } }
}

at runtime a new type will be created:

Class GenXXXList : List
{
int IList.Count { get {... } }
}

and all the code refered to Gen<Twill be replaced with GenXXXList.
(same
way as anonymous methods)

So a Gen<Listused in one assembly wouldn't the same type as a
Gen<Listin another assembly? Sounds like a recipe for hard-to-
diagnose bugs.

--
Jon Skeet - <sk***@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
Sep 28 '07 #6
Ido Samuelson <id***********@gmail.comwrote:
Further, it is never a good idea to have the same class name, same type and
same namespace in different assemblies. So I really don't see a problem in
debugging.
It's completely different to that situation. It's perfectly reasonable
to use List<stringin two different places, but your way of working
would break it. If I were to declare an API of

public Gen<StreamGetGen()

then the object returned by the method wouldn't be usable in a
different assembly.

--
Jon Skeet - <sk***@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
Sep 28 '07 #7
Ido Samuelson <id***********@gmail.comwrote:
isn't that the same case with anonymous methods or extension methods? in
some cases both can cause unexpected behaviors.
No - anonymous methods are only accessed within the same type, and
extension methods are completely different anyway.

Anonymous *types* are consistent within an assembly, but that's mostly
an optimisation - you can't expose an API in terms of anonymous types.

--
Jon Skeet - <sk***@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
Sep 28 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Robert Spoons | last post by:
Can you look over this code, preferably try it, and comment? I believe the 'extend' function below will allow you to use full 'class inheritance' in javascript, but I would like to verify it. ...
18
by: Kamen Yotov | last post by:
hi all, i first posted this on http://msdn.microsoft.com/vcsharp/team/language/ask/default.aspx (ask a c# language designer) a couple of days ago, but no response so far... therefore, i am...
0
by: Jack Addington | last post by:
I have a tree view object that I am trying to load via database calls. I'm trying to make it generic so that I can extend and re-use it. One of the things I want to be able to do is to load the...
30
by: Raymond Hettinger | last post by:
Proposal -------- I am gathering data to evaluate a request for an alternate version of itertools.izip() with a None fill-in feature like that for the built-in map() function: >>> map(None,...
12
by: Raymond Hettinger | last post by:
I am evaluating a request for an alternate version of itertools.izip() that has a None fill-in feature like the built-in map function: >>> map(None, 'abc', '12345') # demonstrate map's None...
2
by: John B | last post by:
If I have the following classes: public abstract class Base { public abstract T CreateItem<T>() where T : Base; } public class Derived : Base { public override T CreateItem<T>()
4
by: =?Utf-8?B?UmljaA==?= | last post by:
Hello, Does vb2005 have a built-in UnDo feature / object for applications so that I can undo actions like other windows apps? Or do I have to write my own UnDo routine? If vb2005 does have a...
8
by: =?ISO-8859-1?Q?m=E9choui?= | last post by:
Problem: - You have tree structure (XML-like) that you don't want to create 100% in memory, because it just takes too long (for instance, you need a http request to request the information from...
10
by: Conrad Lender | last post by:
In a recent thread in this group, I said that in some cases object detection and feature tests weren't sufficient in the development of cross-browser applications, and that there were situations...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.