Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F# 限制参数类型_F# - Fatal编程技术网

F# 限制参数类型

F# 限制参数类型,f#,F#,我试图为条件检查引擎创建一个更紧凑的调用约定。工作示例代码为: type MyType(s:string, i:int) = member val s = s with get, set member val i = i with get, set let StringLenAtLeast10 = fun (s:string) -> s.Length >= 10 let IntAtLeast10 = fun (i:int) -> i &g

我试图为条件检查引擎创建一个更紧凑的调用约定。工作示例代码为:

type MyType(s:string, i:int) =
    member val s = s with get, set
    member val i = i with get, set

let StringLenAtLeast10 =
    fun (s:string) -> s.Length >= 10  

let IntAtLeast10 =
    fun (i:int) -> i >= 10

let SelectS =
    fun (f:MyType) -> f.s

let SelectI =
    fun (f:MyType) -> f.i

type Condition1<'a>(f:'a->bool) =
    member this.IsSatisfied obj = f obj

let cond11 = new Condition1<MyType>(fun f -> StringLenAtLeast10 (SelectS f))
let cond12 = new Condition1<MyType>(fun f -> IntAtLeast10 (SelectI f))
type MyType(s:string,i:int)=
成员val s=s,带有get、set
成员val i=i和get、set
让我们至少10分钟=
乐趣(s:字符串)->s.长度>=10
让我们来看看=
乐趣(i:int)->i>=10
让我们选择=
乐趣(f:MyType)->f.s
让我选择=
乐趣(f:MyType)->f.i
类型Condition1bool)=
成员this.issatified obj=f obj
让cond11=newcondition1(funf->StringLenAtLeast10(选择f))
让cond12=newcondition1(乐趣f->IntAtLeast10(选择f))
我想通过限制参数中间类型来改进它,如下所示:

type Condition2<'a>(selector:'a -> 'b, f:'b->bool) =
    member this.IsSatisfied obj = f (selector obj)

let cond21 = new Condition2<MyType>(SelectS, StringLenAtLeast10)
let cond22 = new Condition2<MyType>(SelectI, IntAtLeast10)
type Condition<'a>(selector, checker) =
    member this.IsSatisfied (obj:'a) = (checker (selector obj)):bool
键入条件2'b,f:'b->bool)=
成员this.issatified obj=f(选择器obj)
让cond21=新条件2(选择,StringLenatlyst10)
让cond22=新条件2(选择i,插入最后10)
然而,它不起作用,可能是因为显而易见的原因。我正在努力,a。改进呼叫约定,是否可能,以及b。找出为什么它无法找出类型关系

/编辑: 正如kvb所指出的,必须显式指定所有类型参数。这就是无法在列表中使用不同类型参数对条件进行分组的问题。因此,我利用了如下类型系统:

type Condition2<'a>(selector:'a -> 'b, f:'b->bool) =
    member this.IsSatisfied obj = f (selector obj)

let cond21 = new Condition2<MyType>(SelectS, StringLenAtLeast10)
let cond22 = new Condition2<MyType>(SelectI, IntAtLeast10)
type Condition<'a>(selector, checker) =
    member this.IsSatisfied (obj:'a) = (checker (selector obj)):bool

type Condition类型的所有类型参数在其定义中都必须是显式的,因此您需要执行以下操作:

type Condition2<'a, 'b>(...) = ...

虽然这样做有效,但我无法将条件分组到列表中,因为类型签名不同。我已经找到了一个相对简单的解决方案,我将在今天晚些时候更新这篇文章。我接受了你的回答,因为它为我指明了正确的方向。