无法定义(?)类型上的运算符重载:
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 Studio2010 Beta2
发布于 2009-10-26 01:53:13
也许有一种更简单的方法(我会看看),但这在紧要关头可以做到:
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" hwhttps://stackoverflow.com/questions/1621250
复制相似问题