首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >F#动态查找运算符(?)重载

F#动态查找运算符(?)重载
EN

Stack Overflow用户
提问于 2009-10-25 23:59:41
回答 1查看 906关注 0票数 3

无法定义(?)类型上的运算符重载:

代码语言:javascript
复制
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,则一切正常:

代码语言:javascript
复制
let (?) (foo : Foo) (name : string) = foo.s + name

let hw  = foo? world

但是我需要为类型Foo直接指定op_Dynamic运算符。第一个代码片段有什么问题?

使用F# 1.9.7.4 @ Visual Studio2010 Beta2

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-10-26 01:53:13

也许有一种更简单的方法(我会看看),但这在紧要关头可以做到:

代码语言:javascript
复制
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
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1621250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档