News for hackers. Stuff that matters.
Re:6 Months with C# by MatthewPhillips on Sunday March 10, 2013 @10:29AM (#5352044)
Microsoft is trying too hard to make C# answer to every single technology trend, adding dynamic type, asynchronous methods, etc. In most cases you wind up with a working but much worse alternative to the real thing. For example, here's how you create an anonymous function that returns a string in C#:

  (new Func<string>(() =>
  {
    return "foo";
  }))();
That's pretty awful and as a result you rarely see anyone doing stuff like that, whereas with JavaScript it's used regularly. But for C# it's another checkbox that they can say they support.

C# should stick with trying to be the best statically typed compiled object oriented language.

Re:6 Months with C# by nkohari on Sunday March 10, 2013 @12:15PM (#5352398)
You're right, that is awful. It's also unquestionably the worst way to write it. Try this instead:

   () => "foo"
Please don't participate in a conversation if you aren't willing to spend the time necessary to gain a baseline level of understanding of the topic.
Re:6 Months with C# by MatthewPhillips on Sunday March 10, 2013 @12:24PM (#5352434)
That's not equivalent code, it doesn't execute itself.

That it simply returns a string with no logic and therefore can be written slightly simpler belays the point I was making. You simply don't see this type of code being used much in C# because it isn't idiomatic, yet it exists to mark off a checkbox.

Re:6 Months with C# by nkohari on Sunday March 10, 2013 @01:23PM (#5352626)
Fair enough, if you want to automatically execute an anonymous function, your example isn't idiomatic C# either. The idiomatic version would look like this:

    var func = () => "foo";
    func();
As far as "you don't see this type of code being used much", that's just patently false. Anonymous functions (particularly predicates) are the whole way you interact with LINQ.

So I'll ask this: what's the equivalent example in Java?

Re:6 Months with C# by MatthewPhillips on Sunday March 10, 2013 @02:29PM (#5352875)
Your example won't even compile. You're confusing functions with lambda expressions. Lambda expressions look a little like a function but they are something different. Lambda can be compiled into functions at run time and that's often what they're used for, but they can't be implicitly assigned to a var. Doing it you're way would look like this:

    Expression<Func<string>> func = () => "foo";
    func.Compile()();
    
That's hardly better, and it's still not anonymous! You probably like Lambda Expressions because they make things like LINQ elegant, and indeed they do. But a lambda expression is confusingly similar looking to a func (indeed a mistake you made) but when you try to do something more complicated like this:

    Expression<Func<string>> func = () =>
    {
      string foo = "bar";
      return foo;
    };
    
That won't compile because a lambda expression can't have a statement body, it can only contain (a single!) expression.

Now the cool thing about lambda expressions is that to the end user they are simple and elegant assuming you know that they are not the same thing as functions, but on the other end of it; consuming a lambda expression you get to see the guts of it and realize what a hack it is on the language. Sure, in the context of a compiled statically typed language it's rather powerful. It's how they are able to compile a lambda expression into a SQL statement, because you can inspect lambda expressions which you can't do to functions.

Now you asked about Java and I'm not going to defend Java or predict what their intentions are, but I'll just say that Java is being much more conservation about sticking to what it is; a statically typed compiled language. C# is attemping to be everything to every body.

Re:6 Months with C# by nkohari on Sunday March 10, 2013 @03:30PM (#5353118)
You're right, I'm a bit rusty, not having written anything large in C# for a couple of years. The compiler can't infer the type, so it would be:

    Func<string> foo = () => "foo";
    func()
I'm well aware of the difference between an anonymous function and an expression in C#. What in your example suggests that you should be using an expression? In practice, you never need to work with expression trees unless you're doing metaprogramming of some kind. They were added to the framework primarily to support metaprogramming and support the DLR.

LINQ (or at least "LINQ to Objects") does not use expression trees. For example, this is the signature of the Select() extension method:

    public static IEnumerable<TResult> Select<TSource, TResult> (this IEnumerable<TSource> source, Func<TSource, TResult> selector) { ... }
    
Note the lack of Expression<Func<>>.

Things like LINQ to SQL and the Entity Framework translate these statements into expression trees, but they do it transparently inside the framework.

My point in bringing up Java is that in order to do something like what we're talking about, you'd have to jump through a whole bunch of hoops. It certainly would be more difficult than in C# -- which is really what matters more than the "purity" of the language.

Re:6 Months with C# by nrkn on Sunday March 10, 2013 @06:40PM (#5353858)
You're wrong. A lambda can have a body. Compiles and executes:

  new Func<string>(() => {
    var foo = "bar";
    return foo;
  })();
Re:6 Months with C# by dennisgorelik on Sunday March 10, 2013 @03:23PM (#5353096)
This compiles:

  Func<string> func = () => "foo";
  string result = func();
Re:6 Months with C# by nrkn on Sunday March 10, 2013 @06:34PM (#5353842)

    new Func<string>(()=>"foo")();

    (function(){return "foo";})();
Oh look - exactly the same number of characters. How awful.
Re:6 Months with C# by yread on Sunday March 10, 2013 @11:08AM (#5352178)
eh? How about

       ()=>"foo"

?
Re:6 Months with C# by MatthewPhillips on Sunday March 10, 2013 @11:10AM (#5352188)
Picking on the simplicity of my example completely misses the point.
Re:6 Months with C# by sultezdukes on Sunday March 10, 2013 @11:06AM (#5352174)
no, that piece of code that you contrived up is awful, not .NET's anonymous functions.