首页 > 解决方案 > Self referencing record types in Standard ML

问题描述

I would to create a record type declaration such as

type 'a cx = { foo : string, handler : 'a cx -> 'a cx }

but this code does not compile.

I also tried "mutually recursive type synonym declarations", kinda

type 'a cx = { foo : string, handler : 'a hnd }
and 'a hnd = 'a cx -> 'a cx;

with no success.

In Haskell this would be

data Cx a = MkCx { foo :: String, handler :: Cx a -> Cx a }

How can I achieve that in SML?

UPDATE

It is possible with mtually recursive datatypes

datatype 'a cx = MkCx of string * ('a hnd)
and 'a hnd = MkHnd of 'a cx -> 'a cx;

but it is ugly and there's no nice record syntax with unordered access.

标签: typesml

解决方案


最接近您的尝试是:

datatype 'a cx = CX of { foo : string, handler : 'a hnd }
withtype 'a hnd = 'a cx -> 'a cx

但是,这需要模式匹配才能访问记录。定义访问器函数可能会更方便。


推荐阅读