Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# Static Methods In Class

I'm trying to figure out how to make static methods in a class in F#. Does anyone have any idea how to do this?

like image 841
RCIX Avatar asked Jun 13 '09 08:06

RCIX


2 Answers

Sure, just prefix the method with the static keyword. Here's an example:

type Example = class
  static member Add a b = a + b
end
Example.Add 1 2

val it : int = 3
like image 82
sker Avatar answered Oct 09 '22 07:10

sker


If you wanted to have static methods in a static class, then use Module

Check out this link, in particular the Modules section:

http://fsharpforfunandprofit.com/posts/organizing-functions/

Here's a module that contains two functions:

module MathStuff = 

    let add x y  = x + y
    let subtract x y  = x - y

Behind the scenes, the F# compiler creates a static class with static methods. So the C# equivalent would be:

static class MathStuff
{
    static public int add(int x, int y)
    {
        return x + y;
    }

    static public int subtract(int x, int y)
    {
        return x - y;
    }
}
like image 39
xx1xx Avatar answered Oct 09 '22 09:10

xx1xx