2011-10-26 16 views
1

私はずっと前からC#devです。C#からF#クラスへの移行

私はF#を学び、科学的目的/研究/認可のために使用します。私は多くの機能的な機能が強力だが、私はクラスを書くのに戸惑いしている.C#で非常にうまくいくことがわかっている。

以下のC#コードを翻訳するのに役立つものは何ですか?

class CoolAttribute : Attribute 
{ 
} 

class BaseClass 
{ 
    public BaseClass(int zzz) 
    { 
     // zzz doesn't matter; what matters is that zzz is required 
    } 

    public virtual void XHello() 
    { 
     Console.WriteLine("I'm BaseClass."); 
    } 
} 

interface IFirst 
{ 
    void Hello(); 
} 

interface ISecond 
{ 
    int MagicNumber(); 
} 

[DebuggerDisplay("pubI = {pubI}")] 
class SampleClass : BaseClass, IFirst, ISecond 
{ 
    private int privI; 
    protected int protI; 
    public int pubI; 

    private static string s_privStr; 

    static SampleClass() 
    { 
     s_privStr = ""; 
    } 

    public event Action<string> OnSomething = (el) => { }; 

    public int PubI 
    { 
     get { return pubI; } 
     set { pubI = value; } 
    } 

    public static string StatStr 
    { 
     get { return s_privStr; } 
    } 

    // Default constructor 
    SampleClass() 
     : base(0) 
    { 
     privI = 1; 
     protI = 2; 
     pubI = 3; 
    } 

    // Other constructor 
    SampleClass(int a, int b, int c) 
     : base(a + b + c) 
    { 
     privI = a; 
     protI = b; 
     pubI = c; 
    } 

    [Conditional("DEBUG")] 
    public void PubSimpleMethod() 
    { 
    } 

    protected virtual void ProtVirtMethod() 
    { 
     OnSomething("From the virt method."); 
    } 

    private static void PrivStatMethod() 
    { 
    } 

    public void Hello() 
    { 
     Console.WriteLine("Hello (IFirst)"); 
    } 

    public override void XHello() 
    { 
     Console.WriteLine("I'm SampleClass"); 
    } 

    public int MagicNumber() 
    { 
     return privI + protI + pubI; 
    } 

    public void Additional([Cool] int i) 
    { 
    } 

    public static SampleClass operator +(SampleClass a, SampleClass b) 
    { 
     return new SampleClass(a.privI + b.privI, 
      a.protI + b.protI, 
      a.pubI + b.pubI); 
    } 
} 

F#の翻訳[進行中の仕事は、答えを更新する]:

//// wrong ... 
//type CoolAtribute = 
// extend Attribute 

type BaseClass(zzz : int) = 
    // virtual 
    abstract XHello : unit -> unit 
    default this.XHello() = 
     printfn "I'm BaseClass." 

// seems Ok 
type IFirst = 
    abstract Hello : unit 

type ISecond = 
    abstract MagicNumber : int 

[<DebuggerDisplay("pubI = {pubI}")>] // ???? 
type SampleClass() = 
    inherit BaseClass(0) // necessary argument ? 1 constructor 
    // implements IFirst, ISecond 

    let mutable privI = 0 // initialization required 
    // protI 
    // pubI 

    // wrong: 
    //let static mutable s_privStr = "" 

    // static constructor 

    // event OnSomething 

    // seems Ok 
    member this.PubI 
     with get() = privI 
     and set(value) = privI <- value 

    // ?? 
    //static member StatStr 
    // with get() = s_privStr 
    // and set(value) = s_privStr <- value 


    // Default constructor 


    // Other constructor 
    // C#: SampleClass(int a, int b, int c) 

    [<Conditional("DEBUG")>] 
    member this.PubSimpleMethod() = 
     do ignore 

    abstract ProtVirtMethod : unit -> unit 
    //protected 
    default this.ProtVirtMethod() = 
     // raise event OnSomething("From the virt method."); 

    //private 
    //static 
    member this.PrivStatMethod() = 
     do ignore 

    member this.Hello() = 
     printfn "Hello (IFirst)" 

    // override 
    member this.XHello() = 
     printfn "I'm SampleClass" 

    member this.MagicNumber() : int = 
     privI + protI + pubI 

    // apply attribute to the argument 
    member this.Additional((*[Cool*) i :int) = 
     do ignore 

    // operator + 
+0

は、http参照:// lorgonblog.wordpress.com/2009/02/13/the-basic-syntax-of-f-classes-interfaces-and-members/ – Brian

答えて

5

コードのほとんどは、私にはよさそうだが、あなたは仮想メソッドを定義するために、プレーンmemberを使用することはできません。 (これは、F#コードでは一般的に実装の継承を使用しないため、これは頻繁には必要ありません)。仮想メソッドを定義するには

、あなたがabstractdefaultを使用する必要があります。

type Virtual() = 
    abstract Foo : int -> int 
    default this.Foo(n) = n + 1 

派生型でメソッドをオーバーライドするには、次のように記述することができます:

type Derived() = 
    inherit Virtual() 

    override this.Foo(n) = n + 2  
関連する問題