ocaml中的{X with value} [英] {X with value} in ocaml

查看:94
本文介绍了ocaml中的{X with value}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Yacfe示例中看到了以下函数调用:

  Visitor_c.vk_program { Visitor_c.default_visitor_c with
    Visitor_c.kexpr = (fun (k, bigf) exp -> 
      match Ast_c.unwrap_expr exp with
      | Binary(e1, Logical (Eq), (((Constant(Int("0")) as _e2),_t),ii)) -> 

          (match Ast_c.get_onlytype_expr e1 with 
          | Some (qu, (Pointer _,_ii)) -> 

              let idzero = Common.tuple_of_list1 ii in
              idzero.cocci_tag := 
                Ast_cocci.MINUS (Ast_cocci.NoPos, [[null_addon]]), [];

          | _ -> k exp
          )
      | _ -> k exp
    );
  } ast;

我可以看到一个函数调用,其中record初始化为第一个参数,而ast初始化为第二个参数.

I can see a function call with record initialized as the first argument, and ast as the second argument.

我不熟悉表单的语法:

{Visitor_c.default_visitor_c with Visitor_c.kexpr = some_value;}

这是什么意思?我知道记录可以像{name=value;name=value;...}一样初始化,但是我对{X with name=value}不熟悉,您能告诉我它的意思吗?

What does this means? I know a record can be initialized like {name=value;name=value;...}, but I'm not familiar with the {X with name=value}, can you tell me what it means?

我在 Ocaml手册除以下内容外,没有其他关于合法记录值初始化的信息:

I can't find in the Ocaml Manual nothing about legal record value initialization other than the following:

6.2.3记录

6.2.3 Records

记录值标记为的元组 价值观.记录值写成{ field1 = v1; …; fieldn = vn} 将值vi与记录关联 场fieldi,对于i = 1…n.这 当前的实施支持 最多222 − 1个字段的记录 (4194303个字段).

Record values are labeled tuples of values. The record value written { field1 = v1; …; fieldn = vn } associates the value vi to the record field fieldi, for i = 1 … n. The current implementation supports records with up to 222 − 1 fields (4194303 fields).

如果您在答案中包含对OCaml手册中相关部分的引用,我会感到很高兴.

I'll be glad if in your answer you'll include a reference to the relevant section in the OCaml manual.

推荐答案

有时称为记录更新"或功能更新"或类似的东西.它计算出一个与X相同类型的新记录,并且其字段初始化为与X中的字段相同,但在"with"之后列出的字段将被初始化为给定的值.

This is sometimes called a "record update" or "functional update" or something like that. It evaluates to a new record of the same type as X, and whose fields are initialized to the same as those in X, except the ones which are listed after the "with", which are initialized to those given values.

它对于不可变的记录很有用,因为它是获取此类记录并更改其上一两个东西(以命令式语言通常会更改字段)的便捷方式,而不必列出所有要修改的字段不变.

It is useful for immutable records, as a convenient way to take such a record and change one or two things on it (which in an imperative language you would typically mutate the fields), without having to list out all the fields that are not changed.

OCaml手册第6.7.3节,向下滚动到记录",第二段.

It is described in the OCaml manual section 6.7.3, scroll down to "Records", second paragraph.

对于熟悉Haskell的人,OCaml语法

For those who are familiar with Haskell, the OCaml syntax

{ expr with field1 = expr1 ; ... ;  fieldn = exprn }

与Haskell语法相同

is the same as the Haskell syntax

expr { field1 = expr1 , ... ,  fieldn = exprn }

这篇关于ocaml中的{X with value}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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