Declare private static members in F#?
- by acidzombie24
I decided to port the class in C# below to F# as an exercise.
It was difficult. I only notice three problems
1) Greet is visible
2) I can not get v to be a static class variable
3) I do not know how to set the greet member in the constructor.
How do i fix these? The code should be similar enough that i do not need to change any C# source. ATM only Test1.v = 21; does not work
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CsFsTest
{
class Program
{
static void Main(string[] args)
{
Test1.hi("stu");
new Test1().hi();
Test1.v = 21;
var a = new Test1("Stan");
a.hi();
a.a = 9;
Console.WriteLine("v = {0} {1} {2}", a.a, a.b, a.NotSTATIC());
}
}
class Test1
{
public int a;
public int b { get { return a * 2; } }
string greet = "User";
public static int v;
public Test1() {}
public Test1(string name) { greet = name; }
public static void hi(string greet) { Console.WriteLine("Hi {0}", greet); }
public void hi() { Console.WriteLine("Hi {0} #{1}", greet, v); }
public int NotSTATIC() { return v; }
}
}
F#
namespace CsFsTest
type Test1 =
(*
public int a;
public int b { get { return a * 2; } }
string greet = "User";
public static int v;
*)
[<DefaultValue>]
val mutable a : int
member x.b = x.a * 2
member x.greet = "User" (*!! Needs to be private *)
[<DefaultValue>]
val mutable v : int (*!! Needs to be static *)
(*
public Test1() {}
public Test1(string name) { greet = name; }
*)
new () = {}
new (name) = { }
(*
public static void hi(string greet) { Console.WriteLine("Hi {0}", greet); }
public void hi() { Console.WriteLine("Hi {0} #{1}", greet, v); }
public int NotSTATIC() { return v; }
*)
static member hi(greet) =
printfn "hi %s" greet
member x.hi() =
printfn "hi %s #%i" x.greet x.v
member x.NotSTATIC() =
x.v