Back to school : Getting to know F#

This post starts with a basic introduction of F# and finally ends up writing a simple unit test for an F# member. For those who don’t know what F# is all about, Its a product from Microsoft research and now part of VS 2010 family. Actually from Wikipedia it is described as follows:

F# (pronounced F Sharp) is a multi-paradigm programming language, targeting the .NET Framework, that encompasses functional programming as well as imperative object-oriented programming disciplines. It is a variant of ML and is largely compatible with the OCaml implementation. F# was initially developed by Don Syme at Microsoft Research but is now being developed at Microsoft Developer Division and is being distributed as a fully supported language in the .NET Framework and Visual Studio as part of Visual Studio 2010.

Now, let’s first start by creating an F# project using the built in VS template:

image

 

Once you hit ok, It will end you up with an empty module project like that follows:

image

Now, first of all what is a module in F# ? From MSDN library it is stated as:

In the context of the F# language, a module is a grouping of F# code, such as values, types, and function values, in an F# program. Grouping code in modules helps keep related code together and helps avoid name conflicts in your program.

But, it looks to me rather a namespace in C#. Next let’s create the first type in F#.

  1. type IMonkey =
  2.     interface
  3.        abstract member Name : string with set, get
  4.        abstract member Echo : unit -> int
  5.        abstract member Execute : unit -> unit
  6.     end

 

In F# there everything is type , if you mark it as interface then use the interface or class keyword. Here is in the code block, we can also see that the first member is declared using the with keyword followed by set, get which states that its a read/write property. The second member has :unit-> int , it means that it takes no argument and returns an integer value. Now, what if we had an argument, then the declaration would look like:

  1. abstract member Echo : arg1 : int -> int

 

Moving on to multiple arguments, it needs to be written in a tupled way:

 

  1. abstract member Echo : arg1 : int * arg2:int -> int

 

The question remains it’s an interface but how we can declare class members . To find out more lets consider this:

  1. type Foo =
  2.     class
  3.       member this.Echo(arg1 : int) =
  4.          10
  5.       static member EchoStatic() = 20
  6.     end

 

Here, type declaration is as expected has a class keyword. But for members there is one instance method and one static method. One returns 10 and another returns  20 but first one has arguments too.  The declaration is slightly different for implemented or class members where the assigned value acts as a return value and you have to specify the argument(s) via colon format and using commas like:

  1. member this.Echo(arg1 : int, arg2: int) = 100

 

Of course you have to specify this keyword to let F#  know that its an instance member. Now, moving further what would happen if I would implement “IMonky” interface to “Foo” class.

 

  1. type Foo =
  2.     class
  3.     interface IMonkey with
  4.         member this.Echo() = 10
  5.         member this.Execute() = ()
  6.         member this.Name with get() = "test"
  7.         member this.Name with set(arg1 : string) = ()
  8.         end
  9.         member this.Echo(arg1 : int) =
  10.          10
  11.         static member EchoStatic() = 20
  12.     end

 

Here, for each interface implemented should be wrapped with the following block :

interface <name> with

//  your implementation.

end

Another thing to notice here that how property getter and setter is implemented using the with keyword. Moreover, the assignment with empty parenthesis  like member this.Execute() = ()  marks that the method is returning anything or a void method.

 

As we are set with declaring an interface, its members and how it is implemented. Now, lets write an unit test that mocks a member of it. I will be using JustMock here but can be done using other tools like NSubstitute (The new player in town) / Rhino. To do so, first we need to add the following lines before the type and following the module declaration.

 

  1. open NUnit.Framework
  2. open Telerik.JustMock

 

Its similar to an using keyword in C#. I am skipping the class declaration and adding instance member into it that can be done in the same way as shown above. Therefore, directly pasting the following code:

 

  1.   [<Test()>]
  2.   member this.ShouldMockMethodCallsWithReturn() =
  3.  
  4.       let monkey = Mock.Create<IMonkey>()
  5.      
  6.       Mock.Arrange(monkey, fun ignore -> monkey.Echo()).Returns(10).MustBeCalled()  
  7.  
  8.       let result = monkey.Echo()
  9.  
  10.       Mock.Assert(monkey)

 

One of the interesting piece here is the variable assignment which is done using let keyword. it’s same like doing this in C#:

var monkey  = Mock.Create<IMonkey>();

In Mock.Arrange  the fun ignore –> monkey.Echo()  is equivalent to  () => monkey.Echo() in C# but can’t use the () as it means differently in F# therefore used an anonymous variable instead.  Everything else is plain old vanilla and hope this introduction to F# helped you a bit that additionally shows a first unit test written with it.

 

Enjoy!!

Leave a Reply

Your email address will not be published. Required fields are marked *

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=""> <strike> <strong>