سؤال

لا يمكن تعريف المشغل الزائد على نوع (؟):

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 (?) (foo : Foo) (name : string) = foo.s + name

let hw  = foo? world

ولكن أنا بحاجة إلى تحديد المشغل op_Dynamic مباشرة لنوع Foo. ما هو الخطأ في التعليمات البرمجية المتكررة الأول؟

وعن طريق F# 1.9.7.4 @ البصرية ستوديو 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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top