First hand experience with F#

I have been studying F# lately and more I read more i fall for functional programming languages.

I have also developing a REST service over last few weeks using Service Stack framework for restfull services in  C#.

To play around with F# i have decided to migrate my same service from C# to F# with ServiceStack  itself .

Below 3  new things for me to explore  at same time

  • — Differences between F#2.0 and F# 3.0
  • — first hand functional programming experience
  • — F# with Service Stack ( console hosting)

Below are are the outcomes of my migration:

  • Clear code
  • Less code about 60% less code
  • No hit and trial , code worked in first compilation
  • Faster development time (over 50% gain)

I will explore more and hopefully i choose this as main programming language for my upcoming Large size project.

Resources for F#

TryF#

http://msdn.microsoft.com/en-us/vstudio/hh388569.aspx

http://fsharpforfunandprofit.com/

http://sergeytihon.wordpress.com/tag/f/

 

 

 

Advertisements

Service Locator example in c#

publicinterfaceICustomer  { string GetCustomerName(); 
 }
  • Implement ICustomer interface.
 classCustomer : ICustomer
    {            publicstring GetCustomerName()          {             return"Customer 1";          	 }      }
  • Create a ServiceLocator as shown below and add the mapping of ICustomer and its real implementation class.
 publicinterfaceIServiceLocator
    {         T GetService<T>();     }      	publicclassServiceLocatorSimple : IServiceLocator
    {    	// map that contains pairs of interfaces and
// references to concrete implementations
privateIDictionary<object, object> services;         	 public ServiceLocatorSimple()          {              	services = newDictionary<object, object>();              // fill the map
this.services.Add(typeof(ICustomer), newCustomer());         }         	 public T GetService<T>()         	 {   try
            {                 	    return (T)services[typeof(T)];               }              catch (KeyNotFoundException)  { thrownewApplicationException("The requested service is not registered"); }          }     }
  • Create a client application to consume it .
 
 staticvoid Main(string[] args)          {           
  IServiceLocator serviceLocator = newServiceLocatorSimple();     
ICustomer customersaervice = serviceLocator.GetService<ICustomer>();  
 Console.WriteLine(customersaervice.GetCustomerName());          }

Now Client application does not know about the concrete implementation. we can easily change the implementation without effecting 
client application.

Embedded statement cannot be a declaration or labeled statement error

“Embedded statement cannot be a declaration or labeled statement” error can drive you crazy sometimes like it did to me .i have a 3-4 lines code(in c#) and it took me enough time to resolve this error .

My code before :

if (args.Length > 0)
HCopen o = new HCopen(args[0]);
else
Console.WriteLine(“No Args”);

After Correction :

if (args.Length > 0) { HCopen o = new HCopen(args[0]); }

else { Console.WriteLine(“Usage: HCopen “); }

Now crux of the story is that code generate Compiler Error CS1023