Thu, 05 Feb 2009
Why You Should Use F# if you use C#
So I've been doing a lot of work in C# lately. That is, for the last six months. In fact I've pretty much been doing nothing but C#, javascript (minus all that icky browser-stuff), and the occasional cmd.exe script. In other words, everything new.
And except for cmd.exe, I actually like all of it. I used to think that C# was just like Java (specifically, just like Java 1.5, the version I learned at school and decided I'd rather not use again thanks very much), so why would I want to use it, but I'm a convert. If you're going to be using a C-syntax-style language, C# seems like the cream. I've also been really grooving on the Javascript too, but that's for a later post.
The things about it that I like in no particular order:
- Delegates and Lambda expressions. Seriously, there are just so many times when you don't want the overhead of defining a function, or when you want the close over a (set of) variable(s) that I really would miss this if I were working in C++ or Java. I'm still not totally clear on when to use one and when to use the other.
- Extension methods. This is just syntactic sugar over the c-style way of doing object oriented programming. Here, you can just define a method, and decorate the first argument with the 'this' keyword, and voila! you can officially pretend that your method was embedded in the original object definition.
- var. Seriously, there are just so many times when you don't want to commit to a type yet. This saves you in two places: the places where the type is manifestly clear from the context, and places where you have a method that returns a type, but you anticipate that type changing frequently as you refactor.
- Object Literals and Collection Initializers. Not as complete as Javascript object literals, but enough that you can define a dictionary literal for example.
Now all that stuff is great. But I'm actually not here to tell you about that. I want to talk about F#. Not because I know it. In fact, I took a book out of the MS Library recently on it, but I returned it almost unopened. (Ditto for Lisp In Small Pieces. I know, I know, take away my geek license now.). But even though the only F# I know is how to end a statement (;;), it has stood me in good, uh, stead. This is because it fills in the gap of the one thing that C# has been missing: a REPL. And this is one thing that F# provides.
So, if you've ever wondered what is the actual string that a System.DateTime.Now.ToString() provides, wonder no longer. Well, what kinds of strings does a DateTime successfully parse? just try it. And no, this isn't the same as just generating a tiny sample program (trust me, after 6 months of learning/using C# on the job, my Visual Studio Projects directory is full of tiny toy projects that exist only to answer a question about the language/syntax/semantics/libraries. But I've replace the need for that with a single empty F# project that I use strictly to provide a REPL. So now I just leave it running all the time, my beloved FSharp_repl project.
So seriously, go and download it now if you're on Windows (and using Visual Studio). It'll save you hours and hours while you're learning. Plus who knows, you may even learn F#!
posted at: 15:28 | path: /Tools | comments/permalink