F#動的ルックアップ演算子(?)のオーバーロード
-
06-07-2019 - |
質問
型に(?)演算子オーバーロードを定義できません:
type Foo =
val s : string
new(s) = { s = s }
static member (?) (foo : Foo, name : string) = foo.s + name
let foo = Foo("hello, ")
let hw = foo? world
// error FS0043: The member or object constructor 'op_Dynamic'
// takes 2 argument(s) but is here given 1. The required signature
// is 'static member Foo.( ? ) : foo:Foo * name:string -> string'.
演算子定義にスタンドアロンのlet-bindingを使用すると、すべて正常に動作します。
let (?) (foo : Foo) (name : string) = foo.s + name
let hw = foo? world
しかし、 Foo
型には op_Dynamic
演算子を直接指定する必要があります。最初のコードスニペットの何が問題になっていますか?
F#1.9.7.4
@ Visual Studio 2010 Beta2の使用
解決
おそらくもっと簡単な方法があるでしょう(私は見ていきます)が、これはピンチで行います:
type Foo =
val s : string
new(s) = { s = s }
static member (?)(foo : Foo, name : string) =
foo.s + name
let inline (?) (o:^T) (prop:string) : ^U =
(^T : (static member (?) : ^T * string -> ^U)(o,prop))
let foo = Foo("hello, ")
let hw = foo ? world
printfn "%s" hw
所属していません StackOverflow