Fantom will eat your nulls!

By: on March 7, 2011

As it is a new year it must be time to learn a new language. Having had a singular lack of success running F# under Mono (well when your two line definition of a simple tree data structure produces a stacktrace it doesn’t bode well) I went back to good old Haskell for a while and then stumbled across an enigmatic comment from Cedric Beust of TestNG fame. This seemed to indicate that a mysterious language existed that ran on top of a JVM, a CLR or Javascript called fantom – so having nothing better to do while jet-lagged in Chicago I decided to investigate.

Fantom is an object / functional hybrid with strong typing or dynamic typing depending how you are feeling.   // This is static
foo->bar(zarjazz) // This is dynamic

If you go with the static typing you get some type inference and nice errors from the compiler, and if you go dynamic you can write your own despatch mechanism for your class and pretend you’re writing Smalltalk or Ruby. You may even be able to go really crazy and add methods that don’t exist as they are called using closures but I haven’t tried that yet.

It has some other nice features inspired by Ruby such as mixin inheritance and passing closures to the methods on its collection libraries so you never have to write a for loop again. Additionally it has a simple build tool, logging and unit testing baked into its core libraries.

So far so good! But the thing that really caught my interest was Nullable types. C# has nullable value types, this has always seemed stupid to me – you take my int which can never generate a NullReference exception, you replace it with an int? which can either contain a null or an int, now NullReference is free to roam your code at will. In Fantom types can only contain null if you let them; by default Fantom types can’t contain a null, however if you add a ‘?’ to the type name it becomes a nullable type – which is like a Maybe in Haskell but just using null as its value for None. This is exactly what I want in my code, if I say I want a Foo the compiler will make sure that I get a Foo and not a null pretending to be a Foo, and if you see a method returning a Foo? you know that you will need to check for nulls before proceeding to use its result or get an error – unlike in Java where really everything that isn’t a primitive type maybe null. The paranoid part of my brain is now thinking, “Oh my! I should null check every parameter, in every method, forever!”

To summarize: so far I like Fantom, it is a nicer and simpler Java / C# and is nowhere near as complicated as Scala. Programming in Fantom feels a lot like programming in Ruby but with the added benefit of having a simple type system present to catch some of your errors.



  1. tonyg says:

    Of course, it’s just like a Maybe in Haskell: Haskell permits Maybe (Maybe a), but there’s no possibility of a??.

  2. Hok says:

    What, so after you do a null check on an “int?” (typically like a conditional, I would imagine?) do you suddenly get a new reference to an “int”?

    And how does the “implicit coercion” between non-nullable and nullable types look like?

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>