I think to be more perl-ish, it needs more random
punctuation characters. ;)
Seriously though, I haven't been paying too much
attention to .NET 2.0, so I just find that friggin'
cool.
I think it's lame. ;)
Reminds me a little about the Visitor Pattern.
Best,
Johan
IMO, I think it would have been nicer if the ForEach
method was an instance method on Array.
Person[] people = new Person[]{..};
people.Each(delegate(Person p)
{
//do something
});
I guess it really doesn't matter, seeing as you can't
modify the base classes anyway ;)
And yeah, I realise that on the generic List, the
ForEach type methods are available.
I'm probably missing something, how is that any
improvement on the .NET 1.x syntax:
Person[] MyArray = new Person[]{..};
foreach(Person p in MyArray) MyFunction(p)
private void MyFunction(Person p)
{
//do something
}
[)amien
Damien: the point is that, using generics , delegate
inference and the new functions, you never have to write
the same logical loop twice.
You could easily write a logical loop that traverses an
array of anything, which will be totally type safe, and
just call array.foreach from anywhere you want on it,
not duplicating the loop, and not having to use
interfaces.
for example:
Array.ForEach(persons,MyFunction<Person>);
Array.ForEach(Tables,MyFunction<Table>);
and of course, having a function that dynamically calls
anothe is now much easier to read and write.
Actual pointers to functions are extremely easy in
Python as well. He might not even need the list. Yes,
Python is my new new language. :)
Because the compiler is infering a delegate for you,
doesnt this mean there will be a slight perf hit over
just foreach'ing over then and calling a function? That
also seems more readable to me than the ForEach syntax.
I think this ForEach method is more useful with
anonymous delegates so that you get access to local
variables...
Cleverguy: The compiler does this at build time, not
run-time. There's no performance penatly for doing this.
Actually, writing my blog post,I stumbled upon another
interesting problem - that there is no way to pass *any*
function.
Imagine this scenario - you take a function in a
parametr. If it takes one parameter, you do something.
If it takes 2 arguments, you do something else.
Stuff like this is not really possible with all this
type-checking.
And this is not quite Perl-ish. These are functional
programming paradigms - originally from Lisp's mapcar