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?
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
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With