473,386 Members | 1,883 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,386 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 4809
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.