c# v3.0: implicit variable declaration, why?

  • Thread starter Thread starter Nathan Laff
  • Start date Start date
Hi Frans,

Frans Bouma said:
Ignacio Machin ( .NET/ C# MVP ) wrote:


That's already the case. Example: I personally never need to use
invoke directly in my code (only very very rarely) and almost never use
anonymous methods. However, there are people who use them regularly.
WHen I see their code, I have to look up the syntaxis sometimes to
understand what's going on, as I'm not familiar with it.

My same case, sometimes people post code here that I have to check the
manual to fully understand (or see where the error is)
For example, can you write, without checking in the manual, a single
line piece of code which filters an List<T> on a given value for a
given property of T?

Not really :)
 
Frans said:
For example, can you write, without checking in the manual, a single
line piece of code which filters an List<T> on a given value for a
given property of T? I always have to lookup the anonymous method
syntaxis to write the predicate out, because I barely use it. The code
I then end up with looks odd to me, similar to these fancy typedefs in
the past which could emit cool code based on a single macro, like

typedef void (APIENTRY * PFNGLTBUFFERMASK3DFXPROC) (GLuint mask);

:P

For a seasoned C developer this is simple, but for a person who rarely
uses typedef, it's hard to read. Similar to anonymous filter methods.
Like sorting a list:
toReturn.Sort( delegate( TemplateBindings a, TemplateBindings b ) {
return -(a.PrecedenceLevel.CompareTo( b.PrecedenceLevel )); } );

I'll be honest. I did have to look up the syntax the first few times I
did, but then I got the hang of it. You're right, the syntax is a bit
tricky. But, I think it's very expressive that you put the predicate
logic exactly where it's used. And in the case of predicates with
dynamic logic you don't have to create a whole new class to capture
variables anymore. Lambda expressions should simply the syntax.
Anonymous methods are just one link in the evolutionary chain that'll
make that possible.

Brian
 
For a seasoned C developer this is simple, but for a person who rarely
uses typedef, it's hard to read. Similar to anonymous filter methods.
Like sorting a list:
toReturn.Sort( delegate( TemplateBindings a, TemplateBindings b ) {
return -(a.PrecedenceLevel.CompareTo( b.PrecedenceLevel )); } );

fancy code, one line, but I personally hate this kind of code, because
it already is complicated to read. And this is C# 2.0. But as I said: I
find it hard(er) to read because I rarely use this syntaxis, but a
person who uses it a lot (because s/he likes it, dunno) it might be
simple and straight forward.

Putting it on one line is probably the mistake here - but the
alternative is *more* complicated, in my view, involving creating a
whole separate class just for the sake of doing the comparison. If you
need the same comparison elsewhere, that would be okay - but otherwise
you're separating the comparison code from the only bit of code which
needs it, essentially just because the syntax for anonymous methods is
a bit clunky.

Fortunately lambda expressions will be a bit nicer with LINQ. It's
easier in a dynamic language with full closure support, of course. In
Groovy I'd just write:

toReturn.sort { x, y -> x.PrecedenceLevel <=> y.PrecedenceLevel }

Now half of the line is basically taken up by repeating the property
name, which is a pity, but if you found yourself doing that often you
could always write a method which took a closure to apply to both
parts, then compare the results. At that stage you could do:

toReturn.sortBy { it -> it.PrecedenceLevel }

Now isn't that nice? :) Yes, if it's the first time you've seen it
there's a certain amount of "eek!"-ness, but I'm sure you can
appreciate how when you're used to closures, they make life really
handy...

(I suspect much of that could be done fairly straightforwardly in C#
3.0 too, but it's likely to be slightly more verbose due to the static
typing.)
 
Back
Top