Question

I am trying Nancy with F#. It pretty cool but I couldn't get it few things in F#. First I ll write down both code.

Here is code of C#

public class HomeModule : NancyModule
{
    public HomeModule()
    {
        this.Get["/"] = (parameters) => View["Index.html"];
    }
}

public class AboutModule : NancyModule
{
    public AboutModule():base("/about")
    {
        this.Get["/"] = (parameters) => "Hello About";
    }
}

Here is code of F#

type HomeModule() = 
    inherit NancyModule()
    let get = ``base``.Get
    let view = ``base``.View
    do get.["/"] <- fun parameters -> view.["Index.html"] :> obj

type AboutModule() = 
    inherit NancyModule("/about")
    let get = ``base``.Get
    do get.["/"] <- fun parameters -> "Hello About" :> obj

Few things I already get, I am putting here just in case someone read this question as reference then s/he will get it.

Do parameter is used as force constructor. It should define after all private or let member of type and before any member of type. As per MSDN. base is equivalent of base in C#. One can use base also but when I did formatting it changed..

So, why base is preferable instead of base. When I tried to called base.View.["Index.html"] within function call it force me to add private member, why? As in C# it just allowed. Now here is the thing that I don't get.

In C# I can directly write Get["/"] but in F# I need to write Get.["/"]. Means I have to call another function instead of property constructor.

Even I don't know how "this" use in constructor internally worked in C#, so if someone explain that, it will great... and if there is any equivalent in F#.

I am also putting nancy code of route builder for reference

public class RouteBuilder : IHideObjectMembers
{
    public RouteBuilder(string method, NancyModule parentModule);

    public Func<dynamic, dynamic> this[string path] { set; }
    public Func<dynamic, dynamic> this[string path, Func<NancyContext, bool> condition] { set; }

    protected void AddRoute(string path, Func<NancyContext, bool> condition, Func<dynamic, dynamic> value);
}

Please let me know if any further details required.

Était-ce utile?

La solution

To give a concrete code snippet, the following example uses as this to name the current instance, so that you can refer to it in the constructor (you can use any name for the variable referring to this) which directly corresponds to the C# version:

type HomeModule() as this = 
  inherit NancyModule()
  do this.Get.["/"] <- fun parameters -> this.View.["Index.html"] :> obj

Autres conseils

Some comments:

For the reason you have to use Get.["/"] in F#, the problem is the compiler needs to differentiate between an accessors and a function which takes a list as an argument (["/"] = "/"::[])

For the base issue, I think you should use base without the extra quotes. You may need to add as self to your constructor to be able to use it though, adding the double quotes makes it into a variable name, which doesn't do what you want.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top