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

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.Collections.Generic;using System.Diagnostics;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.WriteLine("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 2076
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...@yahoo.comwrote:
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;
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
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...
53
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...
26
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...
30
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...
5
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...
1
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...
6
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
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:...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.