News for hackers. Stuff that matters.
Re:Why C# is the best language for mobile development by jordan0day on Wednesday January 02, 2013 @02:28PM (#4998669)
As languages go, C# may be the ultimate "middle ground" because it really excels and providing a middling implementation of [your-favorite-language-feature].

That is, if you're a functional programming enthusiast, you can do a lot of functional programming in C#. Sure, it's often more painful than haskell or lisp or F#, but it's workable.

If you're a dynamic programming enthusiast, C# has a perfectly workable (but ultimately unsatisfying) implementation of dynamic.

Systems programmer? Mark your code as "unsafe" and twiddle bits to your hearts content! It's not as quick or necessarily easy as C, but it's there!

Even its' OOP, which is C#'s "official" paradigm, is probably disappointing to smalltalk guys, but hey, it's better than Java, right?!

It's not glamorous or flashy, it's a workhorse language. And at being a workhorse language, I'd say it really is one of "the best".

Re:Why C# is the best language for mobile development by MichaelGG on Wednesday January 02, 2013 @03:37PM (#4999108)
Then why use C# over F#? F# has a more flexible dynamic system (provide your own implementation), better type inference, and even more unsafe access. So you lose nothing but goto and a bit of loop flexibility.

In theory, a better JIT could provide as-good-as-C output, but in practice you end up screwing around and ending up with less-than-optimal machine code. But F# actually provides an advantage here: inlining. The F# compiler can inline any function, whereas the JIT is somewhat limited in what it will inline. This makes a big difference, as the CLR JIT does a much better job optimizing inside one function than it does cross-function.

Re:Why C# is the best language for mobile development by jordan0day on Wednesday January 02, 2013 @04:26PM (#4999409)
I guess one thing I left out in my post that I should have added was: it's easy for people to learn.

I bet just about anyone with much programming experience can spend 5 minutes looking at C# and understand most of the language. It's familiar enough for anyone from a C/C++/Java/JavaScript/PHP background that it's not intimidating, even at first blush. I bet C# is more comfy for a lot of Ruby and Python folks, too.

I really, really, really like F#. The main problem with F# isn't a language or syntax problem (in fact, it's not a problem at all). F# (or other functional-first languages) isn't just different syntax-wise, it's different thinking-wise. A "good" F# program is generally written in a fundamentally different way than a "good" C# program. This is the declarative vs. imperative split. Sometimes it's really hard to convince people to change the way they think about how to go about solving a problem.

Re:Why C# is the best language for mobile development by benzor on Wednesday January 02, 2013 @03:13PM (#4998971)
Sorry, but you're really attacking a straw man here.

Neither C# itself nor the OP's article sell C# as being a prime language for functional, dynamic, or systems programming. Rather, it has inherited some convenient little bits of each paradigm to make the language that much nicer to write.

For example, don't want to redeclare that "List<List<LongTypeName>>" you're copying over because it has a scope of 2 lines? Just dynamically type it by calling it a "var" and be done with it. Boom: Cleaner code and zero performance hit because the compiler infers the type such that the resulting IL is identical at runtime.

Sure, given features can be misused to produce ugly or "unsatisfying" code, but that's true of any language and is ultimately the programmer's fault. Just because said features are provided, doesn't mean you need to use them. You should ultimately be using the right tool for the job, after all. C# gives you the power of having a wider choice of tools, and I love it.

Re:Why C# is the best language for mobile development by jordan0day on Wednesday January 02, 2013 @04:11PM (#4999332)
Whoa, I'm not attacking anything. I like C# a lot, but I think I'm realistic about it -- it's nobody's perfect language, except in the case of "what's a widely-adopted, multi-paradigm language that I can do typical OO stuff in, but also all that cool dynamic and FP stuff they talk about on hackernews all the time? Also, my coworkers need to be able to understand it?" That's why at the end of my reply I say I think it's one of the best "workhorse" languages.

Anyway, I'm sure others will point this out, but var is NOT dynamic typing. It's type inference, which you seem to know, judging by the last sentence in that paragraph. Dynamic typing doesn't just mean that no type is declared -- it means that no type is inferred (by the compiler).

Re:Why C# is the best language for mobile development by benzor on Wednesday January 02, 2013 @05:06PM (#4999605)
Right, sorry about that, I interpreted your post as being more snarky than it actually was in retrospect. I just love how quickly I get work done with C#, so apologies for getting defensive :)

As for the var feature, you're absolutely right that I'm confusing two things there, because as another commenter pointed out, I use it mainly as a "save myself typing when it doesn't matter" feature.

Re:Why C# is the best language for mobile development by MichaelGG on Wednesday January 02, 2013 @03:27PM (#4999059)
"var" is not dynamic typing. It's just a way to enable type inference - it's still as static as can be. It also has a very limited useful scope in C#, as type inference isn't supported in most areas.
Re:Why C# is the best language for mobile development by TylerE on Wednesday January 02, 2013 @03:35PM (#4999099)
I've long suspected that when programmers ask for dynamic typing, 95% of the time - or more - they are really trying to save FINGER typing, and not data typing.

This is perfectly fine and good - bookkeeping is one of those things that computers are good at.

Re:Why C# is the best language for mobile development by migueldeicaza on Wednesday January 02, 2013 @03:30PM (#4999072)
There is only one place where the type inferencing is not supported, field declarations. Not sure how that became "most areas".
Re:Why C# is the best language for mobile development by MichaelGG on Wednesday January 02, 2013 @03:50PM (#4999194)
Miguel I'm confused then - your experience with Mono certainly means you have a better understanding, but I thought I understood it pretty well, too.

Places lacking type inference:

  - Parameter definitions
  - Method return types
  - Generic type parameters (sometimes)
  - Lambda expressions (due lack of syntax to indicate quoted code)
  - Field declarations
The only place type inference works for declarations is in local variable declarations and lambda parameter types if the delegate type is known.

Perhaps the definition of "most" is arguable, but C# has many places that need you to unnecessarily specify types, and there's no good theoretical reasons for any of it, right? (Except the whole syntax sharing for expression trees versus anonymous functions, which is debatable.)

Re:Why C# is the best language for mobile development by migueldeicaza on Thursday January 03, 2013 @02:27PM (#5004096)
"var" is a shorthand to not declare the type when it can be inferred, in most cases to avoid Foo foo = new Foo(). The point is that it avoid repetitive typing, while still preserving type safety and everything that comes with it: compiler checking that right types are used in the right places.

[Digression While it is possible to use in var foo = GetIt() the practice is discouraged because it makes the type of "foo" opaque to the casual reader]

For a parameter definition, its use clearly makes no sense. What is the type for the following method?

void Demo (var x) {}

"var" in the above example would not really add much value, neither would the following example, where inference will oscillate from useless to fragile, depending on who you ask:

void Demo (var x) { var j = x.Parent; }

For method return types the reason is simple, it serves no useful purpose, in fact, it can be quite damaging as the public API contract can change during routine work. Consider a method:

var Demo (PointF f) { if (f.X < 0) return f.X; return 1; }

The above method signature cal oscillate easily between float or double on the day that someone changes the 1 with 1.2. Reading a diff or a patch file wont catch the fact that you have accidentally changed the type of the function.

If this is the kind of code that you need to write (both cases above), then by all means, use the right tool and replace "var" with "dynamic". I would argue that using "dynamic" for the sake of not declaring the type there is a poor practice, but I am not about to lecture you on poor coding choices.

Generic type paramters: your comment makes no sense.

Lamdba expressions: makes no sense, the parameters are already inferred. Not sure what value (var x) adds over the already existing (x) syntax.

Which brings us to the very case I quoted "field declarations".

The ones that you skipped where it is supported: local variable declarations, for statements, using statements, consts locals and fixed statements.

Re:Why C# is the best language for mobile development by MichaelGG on Thursday January 03, 2013 @05:23PM (#5005085)
Honest question: Are you just playing devil's advocate? It appears that the reasoning for C#'s design is that it targets LINQ as a goal, and the new features (like var and extension methods) were implemented just for the LINQ case, not considering those features by themselves.

In fact, the lack of type inference for fields was said to be due to technical limitations in the C# compiler implementation, not for any language reason. [1] Sure, any product will have limitations due to schedules/resources, but that doesn't change the fact that they're unfortunate limitations that could be fixed. C# hasn't addresses these issues, but they've had 2 or 3 releases since they were introduced.

>For a parameter definition, its use clearly makes no sense. What is the type for the following method? >void Demo (var x) {}

In that case, x has no constraints (it's unused), so it's generic. Pretty simple. An unused parameter isn't that useful outside of a few cases. Usually you'll use the variable, and constraints can be inferred. Or you'll find a constraint that limits the type to a concrete type. Here's some examples:

  void dub (var x) { Tuple.Create(x,x); } // x is T
  void uri (var x) { new Uri(x); } // x is string
  void cmp (var x, var y) { Nullable.Compare(x, y); } // x and y are Nullable<T> where T : struct
The example of "void Demo (var x) { var j = x.Parent; }" is probably one place you'd want it to fail, because using member access to infer types can get complicated, at least within C#'s type system. You'd want a static duck typing system in this case like "anonymous interfaces" or something to that effect.

Arguing against return type inference is wierd: C# does that in anonymous functions - certainly you don't think we should have to annotate types there!

As for type signatures changing, if you need want to keep public contracts, then write them out! No one is saying you need to always infer everywhere, just that it's a great aid. Also, this particular example just demonstrates why C#'s type coercion is a bad idea, albeit understandable, given their desire to follow C style.

Generic type parameters. What do you mean it makes no sense? I can't write, e.g. "new List() { 1 }" - it complains List needs a type parameter. Another example: "Func<var> = () => 1". Nope, I'm required to explicitly provide the type. What if C# didn't have syntax support for Nullable<T>? You'd have to do e.g. "x = new Nullable<int>(1)" or create a helper function like Tuple and have e.g. "x = Nullable.Create(1)". Generic type parameter inference only happens on methods.

Also note that C# can't infer generic type parameter constraints - you've got to annotate them all by hand. Makes sense, I guess, because it doesn't infer any generic type parameter definitions, either.

Lambda expressions can't be type inferred. "var x = () => 1" does not work (CS0815). This is because of the same syntax for expression trees versus anonymous functions. And if that was changed, you'd still need to "promote" func somehow, due to C#'s wierd nominal typing for delegates.

Fixed statements can't be type inferred either: error CS0821: Implicitly-typed local variables cannot be fixed.

The things you mentioned, for (and foreach), using -- those are all local variable declarations. So we have it working for local variables, for some generic type parameters, lambda parameter definitions (when the lambda type is known) and lambda return types.

It's a nice start, and it makes C# far more enjoyable than some other languages like Java. But it's still quite limited, without solid reasons.

1: http://blogs.msdn.com/b/ericlippert/archive/2009/01/26/why-n...

Re:Why C# is the best language for mobile development by kvb on Wednesday January 02, 2013 @03:46PM (#4999161)
There are several other limitations. For instance, method signatures can't be inferred. In F#, you can write

    member this.AddOne(i) = i + 1
and AddOne will be inferred to have the signature (int -> int).

There are plenty of other annoying restrictions, too, such as locals defined by lambdas.

Re:Why C# is the best language for mobile development by amirrajan on Wednesday January 02, 2013 @03:34PM (#4999093)
You're right. The var keyword isn't dynamic typing. The dynamic keyword is though. dynamic C# 4.0 can do mixins, method missing, etc: http://amirrajan.net/Blog/dynamic-c-sharp
Re:Why C# is the best language for mobile development by pretoriusB on Thursday January 03, 2013 @11:15AM (#5002976)
>"var" is not dynamic typing.

I think most people don't care about dynamic typing that much -- they mostly don't want to have to write types (that, an flexible containers).

Re:Why C# is the best language for mobile development by frio on Wednesday January 02, 2013 @04:00PM (#4999252)
He's not attacking anything; the two of you are in (violent) agreement :). He's saying that C# provides all those little bits its "inherited", so it's an acceptably-good language for pretty much anyone, no matter your background.
Re:Why C# is the best language for mobile development by benzor on Wednesday January 02, 2013 @05:08PM (#4999614)
Yeah, as I replied above, I'm a fan and I got overly defensive, so apologies again :)
Re:Why C# is the best language for mobile development by oddthink on Wednesday January 02, 2013 @04:06PM (#4999301)
Just an aside: I don't think you can really say "dynamic programming enthusiast" to mean "dynamically-typed language enthusiast", since "dynamic programming" already means something quite different. It threw me a bit, at least.
Re:Why C# is the best language for mobile development by jordan0day on Wednesday January 02, 2013 @04:28PM (#4999422)
Nice catch, I probably should have said "dynamic programming language enthusiast".
Re:Why C# is the best language for mobile development by nnq on Wednesday January 02, 2013 @02:37PM (#4998725)
Why is C#'s OOP disappointing to Smalltalk guys?
Re:Why C# is the best language for mobile development by dfox on Wednesday January 02, 2013 @03:15PM (#4998989)
Mostly because it looks like Java and on the first glance also shares Java's mostly static type system (which is not strictly true).

The gist of it is: you can send arbitrary messages in Smalltalk (and ObjC) and the object itself deals with them somehow (with most of that being implemented by runtime anyway, but that is different issue). In C# you call methods defined by interface.

Also, you cannot modify arbitrary parts of standard library and runtime, but that is probably for the better (doing that is pretty common in ST world).

Re:Why C# is the best language for mobile development by bigdubs on Wednesday January 02, 2013 @02:40PM (#4998740)
Well, off the top of my head C# doesn't support mixins or multiple inheritance.

You can get around the lack of mixins with extension methods and tight scoping but it's messy.

Re:Why C# is the best language for mobile development by SeanLuke on Wednesday January 02, 2013 @02:53PM (#4998835)
> Well, off the top of my head C# doesn't support mixins or multiple inheritance.

I was not aware that Smalltalk provided either.

Re:Why C# is the best language for mobile development by dfox on Wednesday January 02, 2013 @03:09PM (#4998944)
There are libraries that add both of these features at once (either by defining new (meta)metaclasses or by simulating same effect through simple copying of methods from base classes.)

On the other hand, Smalltalkers are quite keen on adding new methods to almost any existing system class, so mixins are not often as useful. Also Smalltalk development often relies on IDEs that do pretty extensive code generation behind the scenes and I think that this image based development with no real source code to speak of, and lots of reflection based development tools, is more important aspect of Smalltalk than any OO features.

Re:Why C# is the best language for mobile development by amirrajan on Wednesday January 02, 2013 @03:07PM (#4998933)
That is a false statement. dynamic C# 4.0 can do mixins: http://amirrajan.net/Blog/dynamic-c-sharp
Re:Why C# is the best language for mobile development by bigdubs on Wednesday January 02, 2013 @03:59PM (#4999249)
so, yes this leveraging of dynamic metadata provider technically satisfies the requirements for a mixin, but you're still not applying this mixin to a poco, therefore i don't think it's fair to say the language/spec supports it natively.

this is another example of adding mixins to .net code via 3rd party library: http://remix.codeplex.com/

Re:Why C# is the best language for mobile development by amirrajan on Wednesday January 02, 2013 @04:09PM (#4999320)
"but you're still not applying this mixin to a poco"

Clarify?

Re:Why C# is the best language for mobile development by bigdubs on Wednesday January 02, 2013 @04:33PM (#4999445)
POCO = Plain Old (CLR) Object

The distinction I'm drawing here is what is meant by 'natively supports' and 'supported via a 3rd party library'

Re:Why C# is the best language for mobile development by amirrajan on Wednesday January 02, 2013 @07:51PM (#5000403)
That's what I need clarification on. the dynamic keyword wasn't provided by any 3rd party library (it's part of the C# language specification)... how is System.DynamicObject and IDynamicMetaObjectProvider different then System.Object? All poco's inherit System.Object. Do you consider System.Object as 3rd party? True, by default poco's don't inherit from System.DynamiObject. Does that make it not 'natively supported'?
Re:Why C# is the best language for mobile development by bigdubs on Friday January 04, 2013 @09:24PM (#5011167)
No, what I mean by that is in order to use mixins, in any example I've seen, you need to inherit from a class someone else provided that gives it.

Assembly provides all the mechanisms for the dynamic keyword, I wouldn't say it is a feature of x86.

Re:Why C# is the best language for mobile development by king_magic on Wednesday January 02, 2013 @03:05PM (#4998924)
C#'s (and Java's) lack of multiple inheritance is a virtue, IMO.
Re:Why C# is the best language for mobile development by bigdubs on Wednesday January 02, 2013 @04:04PM (#4999287)
I generally agree that use of mixins / multiple inheritance leads to architectural missteps.
Re:Why C# is the best language for mobile development by nnq on Thursday January 03, 2013 @03:06AM (#5001420)
...aren't they completely different tools? One uses mixins to keep the class hierarchy shallow, with fewer levels of inheritance. OTOH multiple inheritance tends to complicate an overly convoluted class hierarchy most of the time. It's true that in a language like Python people use multiple inheritance to "simulate mixins", but it's to the same good effect of keeping the class hierarchy short and flat, but in C++ at least I've never seen MI used to simplify things, as opposed to mixins that always seem well used...