为什么记录类型定义中不允许使用灵活类型? [英] Why are flexible types not allowed in record type definitions?

查看:89
本文介绍了为什么记录类型定义中不允许使用灵活类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试:

type TS1<'state, 'action> = {
    actions : 'state -> #seq<'action>
    move    : 'state -> 'action -> 'state
    state0  : 'state
}

但是类型检查器不会让我:

But the type checker won't let me:

.../stdin(2,29):error FS0715: Anonymous type variables are not permitted in this declaration

但是,如果我展开灵活类型的定义,那很好:

However, if I unfold the definition of the flexible type, I'm good:

type TS2<'state, 'action, 'actions when 'actions :> seq<'action>> = {
    actions : 'state -> 'actions
    move    : 'state -> 'action -> 'state
    state0  : 'state
}

我对必须添加'actions类型变量感到不满意-由于数学对象不那么明显,它使得与确定性转换系统的连接变得不那么明显.

I'm unhappy with having to add the 'actions type variable—it makes the connection to deterministic transition systems as mathematical objects less obvious.

我看不到在记录定义中允许使用灵活的类型会导致什么问题.有某种危险吗?我还有其他方法可以使我想得到清晰的定义吗?

I don't see what could go wrong by allowing flexible types in record definitions. Is it somehow dangerous? Is there some other way I could get the clarity of definition I would like?

更新.我希望能够在利用已知实现的TS类型上编写函数.也就是说,我希望能够定义一个函数

Update. I was hoping to be able to write functions on TS types that exploits known implementations; i.e., I want to be able to define a function

let has_action a ts s = Set.contains a <| ts.actions s

如果操作成员的类型为actions : 'state -> seq<'action>,则显然不会输入.我可以使用第二个定义来做到这一点,在这种情况下,has_action具有类型

This obviously won't type if the type of the actions member is actions : 'state -> seq<'action>. I can do it with the second definition, in which case has_action has type

has_action : a:'a -> ts:TS2<'s,'a,Set<'a>> -> s:'s -> bool when 'a : comparison

此示例的类型表明,TS1中的灵活类型可能无济于事.我无法避免在TS2中出现混乱的第三类型参数吗?在我看来,状态操作集合的确切实现是不应在类型中公开的实现细节.

The type of this example suggests that the flexible type in TS1 perhaps wouldn't help. There is no way I can avoid the messy third type parameter in TS2? It seems to me the exact implementation of the collection of actions for a state is an implementation detail that should not be exposed in the type.

推荐答案

这仅是当前编译器对允许的记录签名种类的实现的限制.例如,如果您在概念上以相同的方式定义类型,但使用接口或抽象类而不是记录,则可以很好地编译:

This is just a limitation of the current compiler's implementation of the permitted kinds of record signatures. For instance, if you define your type in the same way conceptually, but using an interface or an abstract class instead of a record, it compiles fine:

type TS1<'state, 'action> = 
    abstract actions : 'state -> #seq<'action>
    abstract move    : 'state -> 'action -> 'state
    abstract state0  : 'state

这篇关于为什么记录类型定义中不允许使用灵活类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆