F # dynamische Lookup-Operator (?) Überlastung
-
06-07-2019 - |
Frage
Kann nicht definieren Betreiber Überlastung Typ (?):
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'.
Alle funktionieren gut, wenn ich eigenständige let-Bindung für Operator Definition verwenden:
let (?) (foo : Foo) (name : string) = foo.s + name
let hw = foo? world
Aber ich muss op_Dynamic
Operator direkt für Typ Foo
angeben. Was ist los mit dem ersten Code-Snippet?
Mit F# 1.9.7.4
@ Visual Studio 2010 Beta 2
Lösung
Vielleicht gibt es einen einfacheren Weg (Ich werde sehen), aber dies wird in einer Prise tun:
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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow