Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.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 WorkflowStep<'next, 'prev, 'cancel> = abstract member Next : unit -> 'next abstract member Prev : unit -> 'prev abstract member Cancel : unit -> 'cancel 键入WorkflowStep“prev” 抽象成员取消:单元->取消 我想表达这样一个事实,'next、'

给定以下类型

type WorkflowStep<'next, 'prev, 'cancel> =
     abstract member Next : unit -> 'next
     abstract member Prev : unit -> 'prev
     abstract member Cancel : unit -> 'cancel
键入WorkflowStep“prev”
抽象成员取消:单元->取消

我想表达这样一个事实,
'next
'prev
'cancel
也应该是
工作流步骤
类型或
单元
类型。这是否可以在类型级别上用F#编码?

这是不可能的。如果要实现这一点,这将意味着工作流本身将包含无限的泛型序列,这在f#中是不受支持的

下面更详细地解释了为什么要使用上面提供的工作流类型的简化版本

//error workflow must be provided a type
type Workflow<'t when 't :> Workflow<_>> = 
    abstract member Next : unit -> 't
//必须为错误工作流提供一个类型
键入工作流>=
下一个抽象成员:unit->'t
但是,当我们指定类型参数时 对于返回的工作流,现在需要应用2个参数

//workflow now requires 2 parameters
type Workflow<'nextvalue, 't when t:>Workflow<'nextvalue>>
    abstract member Next : unit->'t
//工作流现在需要2个参数
键入WorkflowWorkflow选项
抽象成员上一个:单元->工作流选项
您在上面看到的选项类型是,您可以返回值(Some)或单位(None)


注意,这种类型的类型列表确实有一个名称,它是用某种语言实现的,称为可变类型

除非您的描述缺少一些细节,使其不可行,否则有一种相当简单的编码,甚至不要求类型为泛型:

type Transition =
    | Step of WorkflowStep
    | Done

 and WorkflowStep =
     abstract member Next : unit -> Transition
     abstract member Prev : unit -> Transition
     abstract member Cancel : unit -> Transition
其中,
Transition
捕获您对
WorkflowStep
的需求,以生成另一个步骤或单位值。这为您提供了一种反向的、类似CPS的流量控制机制

type Transition =
    | Step of WorkflowStep
    | Done

 and WorkflowStep =
     abstract member Next : unit -> Transition
     abstract member Prev : unit -> Transition
     abstract member Cancel : unit -> Transition