6 ms·
I'm curious how this handles the very common case of passing functions as parameters in JavaScript. I know it's possible in C# but it's much more difficult to
by Osiris 12y ago
I'm curious how this handles the very common case of passing functions as parameters in JavaScript. I know it's possible in C# but it's much more difficult to do. JavaScript is a much more functional language than C# and the design patterns are quite different.
- grokys 12y agoIn what way is it more difficult in C#? C# has lambda syntax: obj.Function(() => { ... });
- recursive 12y agoI'm curious why you think it's more difficult. Aside from static vs dynamic typing issues, it's pretty much identical.
- deleted 12y ago[deleted]
- rjbwork 12y agoC#: Func<string> blah = () => "im a function"; JavaScript: var blah = function(){ return "im a function"}; Not only is it not harder, it's more terse in C#.
- grokys 12y agoThat can be made even simpler in C#: var blah = () => "im a function";
- kozhevnikov 12y agoIs that a new C# 6 feature? Cause in C# 5 delegates can't be implicitly typed. Func<string> blah = () => "im a function"; var blah = new Func<string>(() => "im a function");
- Gurkenmaster 12y agoActually C#6 looks more like this: string blah() => "im a function"; Ceylon: String blah() => "im a function"; Coincidence? Just kidding.
- jacabado 12y agoYou just can't do it implicitl because the compiler can't distinguish Func<string> from Expr<Func<string>>. But you can have a generic extension method that returns one of the types and then via inference you can do it implicitly typed. It shoud be something like this in C# 5: public static class Lamb { public static Func<T> da(this Func<T> f) { return f; } } and then: var egocentricFn = Lamb.da(() => "im a function"); I find it really useful not for Func's but for Expressions, as the type declarations can get pretty long.
- rjbwork 12y agoI actually had that up there at first, but remembered you can't implicitly type lambdas yet.
- antihero 12y agoIn ECMAScript 6: var blah = () => "im a function"; http://is.gd/D42hDi http://is.gd/D42hDi :)
- deleted 12y ago[deleted]
- dguaraglia 12y agoWow. I love it when good syntax sugar is inherited by languages.
- antihero 12y agoYep, I've popped 6to5 in our gulp build process (it also handles JSX which is bleedin' fantastic), and my team and I are absolutely loving it. The fact that, for instance, you can use ES6 modules, and it will be converted to whichever module loading system you're actually using (AMD or Browserify, for instance), means that our code is actually more portable now (our React/Flux/browseify app may well have to be integrated into a project that uses backbone/AMD soon, so that's no longer a headache.
- Osiris 12y agoMy C# skills must be out of date. Previously you had to use delegates and it was pretty cumbersome.
- jaked89 12y agoEven with delegates, C#'s version is pretty much equivalent to JS's: JavaScript: var blah = function(){ return "im a function"}; C# 3.0: var blah = delegate { return "im a function"; }; C# 2.0: Func<string> blah = delegate { return "im a function"; };