Posted By: Pop Catalin Sever | Mar 7th, 2006 @ 1:34 AM
page 1 of 1
Comments: 21 | Views: 4824
Anyone at Microsoft ever considered to include generic type inheritance in the next release?

What I mean is someting like this :

 class MyClass<T> : T {
}

where the inheritance chain is chosen when the type is constructed.

I personaly forsee this to be very useful and i'll give a small example : 

Let's say an ipothethical scenario where I want to give an arbitrary class the ability to store state and snapshot info. Ofcourse I could do this by simply subclasing it or create a generic class to be a container ofering those capablities, but :

         - in the first case the same implemetation must be done for every new type (Copy code)

         - in the second case the ineritance chain is lost so the container type can't be used in place o the actual type.

Other useful usages caul be somethin like DataTable<Person> or DataTable<Company> where the DataTable<> type ads an ItemArray property, and RowVersionPropery etc. an actualy makes extens the class so it can be added to an datatable ...

Anyone else had those kind of ideeas before?

footballism
footballism
Another Paradigm Shift!
Pop Catalin Sever wrote:

 class MyClass<T> : T {
}

This is really a bad and implausible idea, you know, when you can do type inheritance this way:
class MyClass<T> : T {
}

Which means that you can also legitimately do this:
    MyClass<String> genericString = new MyClass<String>();
So the MyClass<String> can derive from String, which breaks the encapsulation of OOP, and what's more, this idea can potentialy result in some security issues.
    So never never think about this stupid idea any more.

Sheva
You can solve this sort of problem with Multiple Inheritance, C# doesn't support this although the CLR does. I think only C++ provides it at the moment.
I was at a meeting in Copenhagen where Anders Hejlsberg refered to this as "mixins". They are quite useful for UI frameworks where you may want a window that inherits behavior and/or styling from several utility classes. In C++ one might use multiple inheritance to solve this, but mixins is somewhat cleaner. Actually the discussion about mixins sprang from a question about why C# didn't support multiple inheritance. Anders also said that while we would not get multiple inheritance, he might consider mixins...
TomasDeml
TomasDeml
Run Chiro, Run!
AndyC wrote:
You can solve this sort of problem with Multiple Inheritance, C# doesn't support this although the CLR does. I think only C++ provides it at the moment.


What? CLI (and IL) supports multiple inheritance? Since when? If I missed something, correct me...Smiley
leeappdalecom
leeappdalecom
.nettter
This is something like recursive generics lol

Your saying a container class that is the same type as the type it consumes?

I dont actually see a use for this apart from recursively creating a data structure that can consume itself.  Why would you need this?
TomasDeml wrote:


What? CLI (and IL) supports multiple inheritance? Since when? If I missed something, correct me...


I thought I read somewhere that it did... apparently not... my bad. Perplexed
leeappdalecom
leeappdalecom
.nettter
Ok i think i see what your saying. One thing springs to mind how would you store these snapshot objects without a container object?

If you modify your enitity to be a container then it wont be an entity anymore.

I dont see why you cant use the BindingList<> class to do something similar.

(tell me if i totally got the wrong end of the stick lol)
leeappdalecom
leeappdalecom
.nettter
Pop Catalin Sever wrote:
leeappdalecom wrote:Ok i think i see what your saying. One thing springs to mind how would you store these snapshot objects without a container object?

If you modify your enitity to be a container then it wont be an entity anymore.

I dont see why you cant use the BindingList<> class to do something similar.

(tell me if i totally got the wrong end of the stick lol)


storing them is't a problem the problem is providing new capability and being able to still use the same objects using the old infrastructure.

also BindingList<T> is a colection class for use with GUI's  maingly not need for its capabilitties here


sorry i think im confused at what your trying to acheive
leeappdalecom
leeappdalecom
.nettter
wait it's all just made sense now lol

Inherit and extend the capabilities of different types using generics rather than extending every type you have created and it still be the same type of object in the end.
footballism
footballism
Another Paradigm Shift!
Okay, you still hold up to your ground, dude:O
Then how do you overcome this situation:

public class SnapshotBase
{
    public Int32 SomeMethod1()
    {
    }
    public Int32 SomeMethod2()
    {
    }
}

public class Snapshot<T> : T
{
    public Int32 SomeMethod1()
    {
    }
    public Int32 SomeMethod2()
    {
    }
}

So now, I have SnapshotBase and Snapshot<T> defined, then if I do something like the following in code:
SnapshotBase snapshotInstance = new Snapshot<SnapshotBase>();

snapshotInstance.SomeMethod1();
snapshotInstance.SomeMethod2();

    then, up to this point, which methods will be called, those in SnapshotBase or those in Snapshot<SnapshotBase>?
As a common sense, we all believe the methods in the base will be called, because we actually use the base class reference to call the methods, and because the derived class reimplement the methods defined in the base(hide by signature), and those methods has nothing to do with those in the base, that is to say polymorphism doesn't work here. now the problem comes, because Snapshot<SnapshotBase> is derived from SnapshotBase, it should inherit the public contracts of its base, but the above sample code doesn't work that way. so if we have generic type derived from its type parameter which is exactly what you proclaim, then the fundamental principle of OOP is violated.

Sheva


footballism
footballism
Another Paradigm Shift!
Duplicated thread withdrawn...
footballism
footballism
Another Paradigm Shift!
    Dude, when you compile your code, the C# compiler will emit a warning, if you want to avoid the warning, you should use new keyword, which means that you want to explicitly hide the base type's method. but in my previous code sample, I don't use the new keyword, but the base type's methods are all hidden by the derived type, which you know is pretty bad.

Sheva
page 1 of 1
Comments: 21 | Views: 4824
Microsoft Communities