Types 标准ML中的自引用记录类型
我希望创建一个记录类型声明,例如Types 标准ML中的自引用记录类型,types,ml,Types,Ml,我希望创建一个记录类型声明,例如 type 'a cx = { foo : string, handler : 'a cx -> 'a cx } 但这段代码并不编译 我还尝试了“相互递归类型同义词声明”,有点像 没有成功 在哈斯克尔,这将是 data Cx a = MkCx { foo :: String, handler :: Cx a -> Cx a } 如何在SML中实现这一点 更新 这是可能的与mtually递归数据类型 datatype 'a cx = MkCx of
type 'a cx = { foo : string, handler : 'a cx -> 'a cx }
但这段代码并不编译
我还尝试了“相互递归类型同义词声明”,有点像
没有成功
在哈斯克尔,这将是
data Cx a = MkCx { foo :: String, handler :: Cx a -> Cx a }
如何在SML中实现这一点
更新
这是可能的与mtually递归数据类型
datatype 'a cx = MkCx of string * ('a hnd)
and 'a hnd = MkHnd of 'a cx -> 'a cx;
但是它很难看,并且没有良好的记录语法来进行无序访问。我最终将不同的类型打包到单个模块中,即
type 'a cx = { foo : string };
type 'a hnd = 'a cx -> 'a cx
signature CX = sig
type t
val cx : t cx
val handler : t hnd
end
我最终将不同的类型打包到单个模块中,即
type 'a cx = { foo : string };
type 'a hnd = 'a cx -> 'a cx
signature CX = sig
type t
val cx : t cx
val handler : t hnd
end
最接近您的尝试是:
datatype 'a cx = CX of { foo : string, handler : 'a hnd }
withtype 'a hnd = 'a cx -> 'a cx
但是,这需要模式匹配来访问记录。定义访问器函数可能会更方便。最接近您的尝试是:
datatype 'a cx = CX of { foo : string, handler : 'a hnd }
withtype 'a hnd = 'a cx -> 'a cx
但是,这需要模式匹配来访问记录。定义访问器函数可能会更方便。使用单个构造函数创建代数数据类型对我在SML/NJ中很有用,无需定义多个类型:
datatype 'a cx = CX of { foo : string, handler : 'a cx -> 'a cx }
type
不起作用,因为它只定义了一个别名(如C中的typedef
),它不能递归。在SML/NJ中,使用单个构造函数创建代数数据类型对我来说很有效,而不需要定义多个类型:
datatype 'a cx = CX of { foo : string, handler : 'a cx -> 'a cx }
type
不起作用,因为它只定义了一个别名(如C中的typedef
),不能递归