在OCaml中具有相似字段的记录 [英] Records with similar fields in OCaml

查看:77
本文介绍了在OCaml中具有相似字段的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

答案中,建议的将元信息附加"到类型的方法是使用记录:

In this answer, the suggested way of "attaching" meta information to types was using a record:

type _foo = ... 
and foo = {n:_foo; m:meta}

但是,如果我想使用元信息包装多个类型,该怎么办?显然,记录类型中的所有字段名称都必须具有不同的名称,并写为:

but what if I have multiple types I'd like to wrap with meta information? Apparently all field names in record types must have different names, and writing:

type _foo = ... 
and foo = {n:_foo; m:meta}
...
type _fooX = ... 
and fooX = {nX:_fooX; mX:meta}

似乎是多余的:/.上课是解决这个问题的唯一方法吗?如果可能的话,我想避免处理类.

seems redundant :/. Classes are the only way to solve this? I'd like to avoid dealing with classes, if possible.

推荐答案

也许可以使用参数化类型.

You can use parameterized type, perhaps.

type 'a wrapped = { base: 'a; extra: meta }

这篇关于在OCaml中具有相似字段的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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