473,398 Members | 2,113 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,398 software developers and data experts.

Extension methods almost allow multiple inheritance via mixins (butnot quite)

When I first heard about these new features, I was very excited as it
would have (if implemented as I had expected) rendered mimicking
multiple inheritance almost painless in C#. Unfortunately, due to a
couple limitations of the language, MI is still not attainable (at
least not succinctly).

1 - Interfaces cannot define data (only properties) [This is not a
limitation of 3.5 obviously but being present from day one has caused
ongoing irritation for several years.]
2 - Extension methods cannot be used to mix in property definitions
(or implement interface methods, or override inherited methods).

Another irritation is that the new 'automatic properties' feature
requires that both the getter and the setter have the same
accessibility level (and in practice, this means making a lot more
things 'public' than you'd really like as if you wanted to make both
getter and setter private, there'd be no need for a property at all!).

Any comments? (or other suggestions or workarounds [besides containment
+wrappers (which I already have a macro to generate). (obviously, the
generated wrapper approach is a maintenance nightmare and produces
many more lines of code to read than I'd prefer)])

--Jonathan
Nov 6 '08 #1
3 4810
johanatan wrote:
When I first heard about these new features, I was very excited as it
would have (if implemented as I had expected) rendered mimicking
multiple inheritance almost painless in C#. Unfortunately, due to a
couple limitations of the language, MI is still not attainable (at
least not succinctly).

1 - Interfaces cannot define data (only properties) [This is not a
limitation of 3.5 obviously but being present from day one has caused
ongoing irritation for several years.]
The whole point of an interface is to define a behavioral contract. Fields
have no place in that. What you really want is true multiple inheritance
from classes, not interfaces extended with fields. Interfaces extended with
fields are just abstract classes.
2 - Extension methods cannot be used to mix in property definitions
(or implement interface methods, or override inherited methods).
Extension methods were never meant to enable any sort of wannabe multiple
inheritance. They serve one particular purpose, which is to enable LINQ.

Anything you can do with extension methods you can do with static methods,
because that's what they are. Extension methods just provide some (vital)
syntactic sugar and overload resolution for this approach. That you are
unable to add new state to classes or override methods is exactly by design.
Another irritation is that the new 'automatic properties' feature
requires that both the getter and the setter have the same
accessibility level (and in practice, this means making a lot more
things 'public' than you'd really like as if you wanted to make both
getter and setter private, there'd be no need for a property at all!).
This is simply false. This works fine:

public string Foo { get; private set; }

What *is* true is that automatically implemented properties must define both
a getter and a setter. Omitting either will get you an abstract property
instead.

--
J.
Nov 7 '08 #2
On Nov 6, 4:26*pm, Jeroen Mostert <jmost...@xs4all.nlwrote:
johanatan wrote:
When I first heard about these new features, I was very excited as it
would have (if implemented as I had expected) rendered mimicking
multiple inheritance almost painless in C#. *Unfortunately, due to a
couple limitations of the language, MI is still not attainable (at
least not succinctly).
1 - Interfaces cannot define data (only properties) [This is not a
limitation of 3.5 obviously but being present from day one has caused
ongoing irritation for several years.]

The whole point of an interface is to define a behavioral contract. Fields
have no place in that. What you really want is true multiple inheritance
from classes, not interfaces extended with fields. Interfaces extended with
fields are just abstract classes.
Valid point. But, I see no harm in included data fields in
interfaces.
2 - Extension methods cannot be used to mix in property definitions
(or implement interface methods, or override inherited methods).

Extension methods were never meant to enable any sort of wannabe multiple
inheritance. They serve one particular purpose, which is to enable LINQ.

Anything you can do with extension methods you can do with static methods,
because that's what they are. Extension methods just provide some (vital)
syntactic sugar and overload resolution for this approach. That you are
unable to add new state to classes or override methods is exactly by design.
I understand what the design is-I'm just saying that it is a little
bit to limiting. Do you have any idea why they will not allow
extension methods to implement interfaces (or override methods)?
Another irritation is that the new 'automatic properties' feature
requires that both the getter and the setter have the same
accessibility level (and in practice, this means making a lot more
things 'public' than you'd really like as if you wanted to make both
getter and setter private, there'd be no need for a property at all!).

This is simply false. This works fine:

* *public string Foo { get; private set; }

What *is* true is that automatically implemented properties must define both
* a getter and a setter. Omitting either will get you an abstract property
instead.
Ahh. thanks for that!! That will clean up some of my code.

Back to the MI issue, in my sleep last night an idea occurred to me:

I could use reflection, emit + generics to get MI. I will have a
class such as:

public class Inheritor<T: T
{
}

which I provide a T to dynamically at runtime (using reflection to
create the instance and emit to create the type). The 'target' class
will derive of Inheritor and provide this dynamically type
representing the chain of classes it wants to inherit from.

The type T will consist of:

A <- B <- C <- ... N where I ultimately wanted to derive from A, B,
C, ... N (and where <- represents inheritance).

So, instead of fanning out for MI, we will instead merely have a very
deep one-dimensional hierarchy. ;-)

--Jonathan
Nov 7 '08 #3
On Nov 7, 7:44*am, johanatan <johana...@gmail.comwrote:
>
public class Inheritor<T: T
{

}
Well C# will not accept this, so I must find another workaround.
Maybe the solution will have to be done completely at runtime (even
the leaf class) using reflection+emit. That's a little bit
unfortunate as the code will be less easy to read.

--Jonathan
Nov 7 '08 #4

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

Similar topics

99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
0
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and...
6
by: gregory lielens | last post by:
Hello, I am currently writing python bindings to an existing C++ library, and I just encountered a problem that I hope has been solved by more experienced python programmers: A C++ class...
30
by: Vla | last post by:
why did the designers of c++ think it would be more useful than it turned out to be?
20
by: km | last post by:
Hi all, In the following code why am i not able to access class A's object attribute - 'a' ? I wishto extent class D with all the attributes of its base classes. how do i do that ? thanks in...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
3
by: Deckarep | last post by:
I recently got into somewhat of a debate with a fellow co-worker. First I asked him if he new of a way to add a method to an existing class like enhancing a class but without using Inheritance. He...
47
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever...
3
by: raylopez99 | last post by:
The headline says it all. Great minds think alike: read the blog below from three years ago, as endorsed by Ritchie, who coinvented C. BTW the below lambda expression code will not work (.Where...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.