F# and using strings for method and class/type names

So after about 2 months of jumping back and forth between languages (Including and not only: Ruby, Python, IronPython, Boo, Scala, and a lot more I don’t remember) I’ve finally settled on F# for more reasons than I will put in this post. BUT one thing that kind of blew me away, and I realize that doesn’t take much (I thought the ending to Terminator 3 was great), was the ability to name methods with string representations… eh wha?

Well I was writing unit tests and stumbled onto the FSUnit “library” (and by “library” I mean a single file). Well on the page there was an example that looked a sumthin’ like this:

[<TestFixture>]
type ``Given a LightBulb that has had its state set to true`` ()=  
  ...

Now if you haven’t used F# just replace “type” with “class”. Essential there is a string/text built name. Not something you see, or at least I haven’t, in any old language. So I went forth and was fruitful and multipled… with unit tests… gross.

[<TestFixture>]
type public ``When Adding``() =
    let returnThing = new MethodResult() :> IResult
    let randomTool = new RandomTool()

    [<Test>]
    member public x.``A Warning Message The Success Flag Is Set To True``() =
        returnThing.AddMessage(new MessageItem(randomTool.RandomString(10), MessageCategory.Warning)).Success |> should be True

And using Resharper’s unit test box thingy it gave a read out of:

  When Adding A Warning Message  
     The Success Flag Is Set To True             Success

Now I’m not sure how useful this is for non unit testing but I think it’s obvious it is to reading Unit Test results. Pretty nice.