如何在F#中的约束中指定可选参数?
假设我有一个方法,它接受一些可选参数,有没有一种方法可以通过泛型约束使用该方法 我想这样做:如何在F#中的约束中指定可选参数?,f#,F#,假设我有一个方法,它接受一些可选参数,有没有一种方法可以通过泛型约束使用该方法 我想这样做: type SomeClass() = member this.SomeMethod(?a:int, ?b:int) = match a,b with | Some x, Some y -> x + y | _, _ -> 0 member this.SomeMethod
type SomeClass() =
member
this.SomeMethod(?a:int, ?b:int) =
match a,b with
| Some x, Some y -> x + y
| _, _ -> 0
member
this.SomeMethod (a:int,b:int) = a + b
// This doesn't work
let inline someMethod (a: ^T when ^T : (member SomeMethod: (?x:int)*(?y:int)->int)) =
(^T : (member SomeMethod: ?int*?int->int) (a,2,3))
let inline someMethod (a: ^T when ^T : (member SomeMethod2: int*int->int)) =
(^T : (member SomeMethod2: int*int->int) (a,2,3))
在F#中可能吗?当然,我马上就找到了答案,但这是给其他人的:
type SomeClass() =
member
this.SomeMethod(?a:int, ?b:int) =
match a,b with
| Some x, Some y -> x + y
| _, _ -> 0
member
this.SomeMethod (a:int,b:int) = a + b
let inline someMethod (a: ^T when ^T : (member SomeMethod: int option*int option->int)) =
(^T : (member SomeMethod: int option*int option->int) (a,Some 2,Some 3))
let inline someMethod2 (a: ^T when ^T : (member SomeMethod2: int*int->int)) =
(^T : (member SomeMethod2: int*int->int) (a,2,3))
let a = new SomeClass()
someMethod a
当然,我马上就找到了答案,但这是给其他人的:
type SomeClass() =
member
this.SomeMethod(?a:int, ?b:int) =
match a,b with
| Some x, Some y -> x + y
| _, _ -> 0
member
this.SomeMethod (a:int,b:int) = a + b
let inline someMethod (a: ^T when ^T : (member SomeMethod: int option*int option->int)) =
(^T : (member SomeMethod: int option*int option->int) (a,Some 2,Some 3))
let inline someMethod2 (a: ^T when ^T : (member SomeMethod2: int*int->int)) =
(^T : (member SomeMethod2: int*int->int) (a,2,3))
let a = new SomeClass()
someMethod a
当然,我马上就找到了答案,但这是给其他人的:
type SomeClass() =
member
this.SomeMethod(?a:int, ?b:int) =
match a,b with
| Some x, Some y -> x + y
| _, _ -> 0
member
this.SomeMethod (a:int,b:int) = a + b
let inline someMethod (a: ^T when ^T : (member SomeMethod: int option*int option->int)) =
(^T : (member SomeMethod: int option*int option->int) (a,Some 2,Some 3))
let inline someMethod2 (a: ^T when ^T : (member SomeMethod2: int*int->int)) =
(^T : (member SomeMethod2: int*int->int) (a,2,3))
let a = new SomeClass()
someMethod a
当然,我马上就找到了答案,但这是给其他人的:
type SomeClass() =
member
this.SomeMethod(?a:int, ?b:int) =
match a,b with
| Some x, Some y -> x + y
| _, _ -> 0
member
this.SomeMethod (a:int,b:int) = a + b
let inline someMethod (a: ^T when ^T : (member SomeMethod: int option*int option->int)) =
(^T : (member SomeMethod: int option*int option->int) (a,Some 2,Some 3))
let inline someMethod2 (a: ^T when ^T : (member SomeMethod2: int*int->int)) =
(^T : (member SomeMethod2: int*int->int) (a,2,3))
let a = new SomeClass()
someMethod a
严格地说,根据语言规范(第142页),可选参数(不是选项类型的参数)不能在方法约束中使用:@Petr,这正是我所使用的。我不知道我必须指定该方法的完整“非规范化”版本,并将选项值传递给可选参数。严格来说,根据语言规范(第142页),可选参数(不是选项类型的参数)不能用于方法约束:@Petr,这正是我一直在使用的。我不知道我必须指定该方法的完整“非规范化”版本,并将选项值传递给可选参数。严格来说,根据语言规范(第142页),可选参数(不是选项类型的参数)不能用于方法约束:@Petr,这正是我一直在使用的。我不知道我必须指定该方法的完整“非规范化”版本,并将选项值传递给可选参数。严格来说,根据语言规范(第142页),可选参数(不是选项类型的参数)不能用于方法约束:@Petr,这正是我一直在使用的。我不知道我必须指定方法的完整“非规范化”版本,并将选项值传递给可选参数。