Skip to main content

Anything you can do ...

·3 mins

For any of you that are aware of the newly updated Xamarin Web site, you may have seen the following:

Objective-C was ahead of its time 30 years ago. C# is ahead of its time today. Anything you can do in Objective-C or Java, you can do in C# with Xamarin—usually more succinctly and with fewer bugs.

What is also true is that F# is way ahead of its time, and you can produce even more succinct code with even fewer bugs than C#!

Take the code snippets from that page.

First up the Objective C version:

@interface Person : NSObject
@property (strong, nonatomic) NSString *name;

@implementation Person
- (id)initWithName:(NSString *)name {
    self = [super init];
    if (self) { = name;
    return self;

+ (NSArray *)getNames
    NSArray *people = @[
      [[Person alloc] initWithName:@"David"],
      [[Person alloc] initWithName:@"Vinicius"],
      [[Person alloc] initWithName:@"Serena"],
    NSMutableArray *names = [NSMutableArray array];
    for (Person *person in people) {
    return names;

Heres the C# version:

class Person : NSObject {
  public string Name { get; set; }
  public static string[] GetNames() {
    var people = new[] {
        new Person { Name="David" },
        new Person { Name="Vinicius" },
        new Person { Name="Serena" },
    return people.Select(person => person.Name).ToArray();

And finally the F# version:

type Person() =
   inherit NSObject()
   member val Name = "" with get, set
   static member GetNames() =
      [| new Person(Name="David")
         new Person(Name="Vinicius")
         new Person(Name="Serena") |]
      |> person -> person.Name)

You can see the F# version is doing exactly the same, although we are using the map function from the Array module rather than the Linq Select extension method.

Its not all about the lines of code though, using F# gives you all many advantages:

  • Using the type system to make sure the code is behaving how you expect before you even compile.
  • Pattern matching in F# is amazing! It can vastly simplify complex control logic, add Active patterns to that and you are ready to take on the world!
  • Problems are approached from a functional perspective which often leads to succinct functions that are easy to reason about, test, and compose.
  • F# emphasizes immutability and functional composition rather than inheritance, again this boils down to simplicity.
  • Features like Type providers can vastly simplify how you deal with data within your application, making access to data really easy and intuitive.

Theres are many areas that F# can really help productivity during development. I hope to write a few more short posts to really bring attention to these. I use F# all the time and often forget how awesome it is until I go back to another language thats missing those features.

Until next time!