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# everything is type , if you mark it as interface then use the interface or less 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 manner:

 

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

 

The question remains it’s an interface but how we can declare class members that has method body and returns real value. 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

 

Generally in C# we do :

  1. public int Echo()
  2. {
  3.     // something important
  4.     return 10;
  5. }

That is translated to F# in the following way :

  1. member this.Echo() =
  2.  // do somehting important
  3.  10

It concludes that you don’t need to use return keyword in F# instead just write the return value.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.

 

Finally, 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 from IMonkey. I will be using JustMock but can be done using other tools like NSubstitute (The new player in town) / Rhino. To start, 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 type declaration and steps of adding new test methods into it that can be done in the same way as shown above. Thus, let’s consider the following snippet:

 

  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 thing here to notice is the variable assignment, where a dynamic variable is constructed using the let keyword which is similar doing the same in C#:

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

In line 6 of 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.  Rest is plain old vanilla that is calling the method and asserting the required.

 

Hope this information was helpful.

Enjoy!!

1 Comment

Comments have been disabled for this content.