473,545 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Extension methods no good, like Lambda expressions (and an examplethat doesn't work)

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.Collecti ons.Generic;usi ng System.Diagnost ics;using
System.Text;

And Visual Studio 2007.

So much for the 'ease' and 'convenience' of Lambda expressions. What
a joke.

RL
int[] a = new int[] { 1, 2, 3, 3, 4, 5, 5, 6 };
var lessThan3 = a.Where(x =x < 3); //compiler error here
'not recognized'
foreach (var val in lessThan3)
{ Console.WriteLi ne("val: {0}", val); }
http://www.interact-sw.co.uk/iangblo...tensionmethods

C# 3.0 - Extension Methods - Monday 26 September, 2005, 10:41 PM

One of the new C# 3.0 language features is 'extension methods'. The
basic idea is that the set of methods available on an instance of a
particular type is open to extension. In effect, we can add new
methods to existing types.

Some people seem to think that this is stupid because, they claim, you
can do the same thing with inheritance. However, the inheritance
approach is rife with problems. Let's look at an example.

Supposing you are dealing with some type Foo that you don't own -
maybe it's part of a 3rd party class library, so you don't get to
modify the class. Suppose that this Foo class doesn't define a method
called Bar. However, let's say you've written a method Bar that does
something useful with a Foo instance. You might decide you'd like to
be able to use the following syntax to invoke your Bar method:

void UseFoo(Foo f)
{
f.Bar();
}

As an aside, my first reaction to this is always: why exactly do you
want to do that? I tend to think that if you've written your own
method that's not a part of Foo but which does stuff to Foo, then the
code should probably reflect that. I think it's more honest, and
easier for someone else to read if your code looks like this:

void UseFoo(Foo f)
{
Bar(f);
}

However, requests for the former style seem to come up often enough
that there's no denying the fact that some people really want to do it
that way, apparently. It's not to my taste, but it takes all sorts.

Dynamic language advocates will sometimes chip in at this point to
claim that they have a 'better' solution. In Python you can
dynamically add new methods to an instance:

def UseFoo(f):
f.Bar = SomeMethod
f.Bar()

Better my arse.

This does circumvent the "I didn't create the object" and the "the
class is sealed" problems. So it's better in the sense that it gets
the job done. But it suffers from being bleedin' 'orrible, which tends
to put me off. Why's it horrible? It changes the instance that was
passed in. Was my caller expecting me to edit the set of methods
available on that instance? What if the caller was using this self
same trick to add a Bar method of their own? We just broke their code.
It seems pretty gross to me to go modifying someone else's object just
for the syntactic convenience of the method I'm writing right now.

Since almost all C# source files have a using System; declaration,
this extension method is effectively going to be available globally -
it augments the set of methods available on the base Object type.
Indeed, Peter Ritchie drew an apt analogy on the DOTNET-CX list:

"Extension methods seems to be a hack to recover from lack (or
removal, considering C++ roots) of nonmember functions"

In practice I suspect I'm being unduly nervous. I think my slightly
negative reaction is similar to the feeling I first got when I saw non-
member functions in C++ proposed as being better than member functions
in certain scenarios. "But that's not OO," I spluttered in my
(relatively) youthful idealism. These days I've learned to be
suspicious of any practice whose sole justification is that it's "more
object-oriented." If that's the only reason something has been done in
a particular way, it's probably a code smell. OO is just a toolkit,
and for certain jobs, other styles (e.g. functional programming)
sometimes offer better tools. Of course, if being "more object-
oriented" offers specific advantages in your scenario, then more power
to you, but being OO should not be an end in itself. The fact is that
not all methods belong in classes. You can see this from the way some
classes are really nothing more than namespaces in disguise. (Take a
look at System.Math for example.)

Aug 18 '08 #1
3 2090
Dennis, not Peter Ritchie, was the responsible party for C.

"raylopez99 " wrote:
The headline says it all. Great minds think alike: read the blog
below from three years ago, as endorsed by Ritchie, who coinvented
C.
Aug 18 '08 #2
On Aug 18, 8:07*pm, raylopez99 <raylope...@yah oo.comwrote:
BTW the below lambda expression code will not work (.Where not
recognized). *I am using the 'standard' using directives using
System;using System.Collecti ons.Generic;usi ng System.Diagnost ics;using
System.Text;
There is no such thing as a "standard" using directive. You use what
you need. In your case, you've forgot to write "using System.Linq" -
Where is a method of class Enumerable, which is in that namespace. By
the way, if you were using VS2008, and if you created a project
targetting .NET 3.5, it'd use that namespace by default in all new .cs
files.
And Visual Studio 2007.
No such thing.
* * "Extension methods seems to be a hack to recover from lack (or
removal, considering C++ roots) of nonmember functions"
Extension methods are not really a hack; they are merely syntactic
sugar. Arguably, the one that would not be needed in a language where
method application is uniform regardless of which argument (if any) is
the receiver - such as Dylan or CLOS - but C# inherited its syntax
from another family of OO languages.
Aug 19 '08 #3
Pavel Minaev wrote:
>
And Visual Studio 2007.

No such thing.
Sorry I meant 2008.
>
� � "Extension methods seems to be a hack to recover from lack (or
removal, considering C++ roots) of nonmember functions"

Extension methods are not really a hack; they are merely syntactic
sugar.
Thanks for the suggestion to use Linq in the using directory; that
worked and the example compiled.

RL
Aug 19 '08 #4

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

Similar topics

99
5820
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...
0
2330
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
53
3614
by: Oliver Fromme | last post by:
Hi, I'm trying to write a Python function that parses an expression and builds a function tree from it (recursively). During parsing, lambda functions for the the terms and sub-expressions are constructed on the fly. Now my problem is lazy evaluation. Or at least I think it is. :-)
26
3446
by: Steven Bethard | last post by:
I thought it might be useful to put the recent lambda threads into perspective a bit. I was wondering what lambda gets used for in "real" code, so I grepped my Python Lib directory. Here are some of the ones I looked, classified by how I would rewrite them (if I could): * Rewritable as def statements (<name> = lambda <args>: <expr>...
30
2119
by: Mike Meyer | last post by:
I know, lambda bashing (and defending) in the group is one of the most popular ways to avoid writing code. However, while staring at some Oz code, I noticed a feature that would seem to make both groups happy - if we can figure out how to avoid the ugly syntax. This proposal does away with the well-known/obscure "lambda" keyword. It gives...
5
2148
by: Octal | last post by:
How does the lambda library actually works. How does it know how to evaluate _1, how does it recognize _1 as a placeholder, how does it then calculate _1+_2, or _1+2 etc. The source files seem a bit complicated so any explanation would be appreciated. Thanks
1
2071
by: Petr Prikryl | last post by:
Do you think that the following could became PEP (pre PEP). Please, read it, comment it, reformulate it,... Abstract Introduction of the mechanism for language extensions via modules written using other languages. Extensions of Python could be done via special interpreter extensions. From Python sources, the special modules would look...
6
1442
by: Bhuwan Bhaskar | last post by:
Hi, I want to know what is anonymas methods and how they can be used. Thanks and regards. Bhuwan
4
3350
by: Steffen Bobek | last post by:
Extension methods are made for use with instances. I'd like to "misuse" them as static methods, too. Let me tell you my ambition: I use an extension method to serialize objects somehow like this: MyObject obj = new MyObject(); obj.ToXmlFile("some directory\\some file.xml"); The method "ToXmlFile" is defined elsewhere far apart from...
0
7401
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...
0
7656
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. ...
0
7808
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...
1
7423
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...
0
7757
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...
0
5972
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...
0
4945
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...
0
3443
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1884
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.