11

次のように定義されたクラスとレコードがあります。

namespace Foo
    type internal MyRecord = 
        {
            aValue1 : int
            aValue2 : int
        }
        static member (+) (left : MyRecord, right : MyRecord) : MyRecord =
            {aValue1 = left.aValue1 + right.aValue1; aValue2 = left.aValue2 + right.aValue2;}

    type internal Bar() =
        member this.Baz() = 
            let myRecord1 = {aValue1 = 2; aValue2 = 3;}
            let myRecord2 = {aValue1 = 7; aValue2 = 5;}
            let sum = myRecord1 + myRecord2 //Does not compile
            0

これはコンパイルに失敗します:

メンバーまたはオブジェクト コンストラクター 'op_Addition' はパブリックではありません。プライベート メンバーは、宣言型内からのみアクセスできます。保護されたメンバーは、拡張型からのみアクセスでき、内部ラムダ式からはアクセスできません。

どちらのタイプも内部です。オペレーターを明示的+に public に設定しても、次のようにはなりません。

static member public (+) (left : MyRecord, right : MyRecord) : MyRecord

機能するのは、演算子の使用をやめ、静的メソッドを使用することです。

namespace Foo
    type internal MyRecord = 
        {
            aValue1 : int
            aValue2 : int
        }
        static member Add (left : MyRecord, right : MyRecord) : MyRecord =
            {aValue1 = left.aValue1 + right.aValue1; aValue2 = left.aValue2 + right.aValue2;}

    type internal Bar() =
        member this.Baz() = 
            let myRecord1 = {aValue1 = 2; aValue2 = 3;}
            let myRecord2 = {aValue1 = 7; aValue2 = 5;}
            let sum = MyRecord.Add(myRecord1, myRecord2) //Does compile
            0

この場合、名前付きメンバーを使用すると問題なく機能するのに、F# コンパイラが演算子の使用に苦労するのはなぜですか?

両方の型を internal ではなく public に変更すると、コンパイル エラーも解決されます。

.NET Framework 3.5 を対象とする F# 3.0 で Visual Studio 2012 を使用しています。

4

1 に答える 1