473,800 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4846
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...@xs4al l.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...@gmai l.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
5928
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 important changes, so in this thread I would like to focus on that issue alone. I have edited the Proposed Syntax example below to take out the changes unecessary to this discussion. I left in the change of "instance variable" syntax (...
0
2354
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 Methods Version: $Revision: 1.34 $ Last-Modified: $Date: 2004/09/03 09:32:50 $ Author: Kevin D. Smith, Jim Jewett, Skip Montanaro, Anthony Baxter
6
3076
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 (let's call it CClass) is binded using classical Python extension API to _PClass, which is accesible through python without any problem. The problem is that I want this class to be extended/extensible in python, and expose the python-extended version...
30
2733
by: Vla | last post by:
why did the designers of c++ think it would be more useful than it turned out to be?
20
10089
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 advance for enlightment ... here's the snippet #!/usr/bin/python
60
4948
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 'target' programming community herein) to get some community input and verify (or not) the following two statements. Few programmers (3 to7%) UNDERSTAND 'Strategic Functional Migration
3
1647
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 asked, "Why on earth would you want to do this? Just inherit." I told him other dynamic languages can do this such as JavaScript where you can just add method to say the Array classes prototype chain. I told him I found this technique often...
47
4046
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 support multiple inheritance. In C++ for instance I noticed that the compiler flags an error if you use the "ref" keyword on a class with multiple base classes. This supports the above quote. However, under the "CodeClass2.Bases" property (part...
3
2103
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 not recognized). I am using the 'standard' using directives using System;using System.Collections.Generic;using System.Diagnostics;using System.Text; And Visual Studio 2007.
0
9551
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10505
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10275
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10253
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10033
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7576
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.