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
그러나 지정해야합니다 op_Dynamic
유형에 대해 직접 연산자 Foo
. 첫 번째 코드 스 니펫에 어떤 문제가 있습니까?
사용 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