types - 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.
解决方案
最接近您的尝试是:
datatype 'a cx = CX of { foo : string, handler : 'a hnd }
withtype 'a hnd = 'a cx -> 'a cx
但是,这需要模式匹配才能访问记录。定义访问器函数可能会更方便。
推荐阅读
- constraints - 雪花的唯一约束有什么意义?
- mailgun - mailgun 通过 SMTP 发送模板电子邮件
- c# - 如何使代理随时间变化 C# .NET Framework
- php - 函数渲染使网站速度减慢 500%!任何人都可以解决这个问题吗?
- java - As400 运行个人 JRE
- javascript - Paypal - 多笔交易退款 -node js - 异步问题
- c++ - Arduino millis() - millis() 如何等于 0 以外的任何值?
- python - 如何在numpy中找到最近的邻居?
- mysql - 有条件地混合 ORDER BY
- localhost - WAMPserver - 将 root 设置为 localhost/mysite(损坏的图像)