Syntax 模块名称后的点语法
有大量语法示例与下面的示例类似Syntax 模块名称后的点语法,syntax,reason,Syntax,Reason,有大量语法示例与下面的示例类似 Json.Encode.(object_([("type", string(m.type_)), ("label", string(m.label))])) 这与调用Json.Encode.object[type,stringm.type,label,stringm.label]有何不同?何时使用一种语法或另一种语法?语法M.expr在表达式expr中本地打开模块M。换句话说,它在表达式中引入模块的所有元素。 例如,在你的情况下 Json.Encode.(obj
Json.Encode.(object_([("type", string(m.type_)), ("label", string(m.label))]))
这与调用Json.Encode.object[type,stringm.type,label,stringm.label]有何不同?何时使用一种语法或另一种语法?语法M.expr在表达式expr中本地打开模块M。换句话说,它在表达式中引入模块的所有元素。
例如,在你的情况下
Json.Encode.(object_([("type", string(m.type_)), ("label", string(m.label))]))
转化为
Json.Encode.object_([
("type", Json.Encode.string(m.type_)),
("label", Json.Encode.string(m.label))
])
当处理DSL(如Json.Encoding引入的DSL)时,这种本地开放语法非常有用,同时仍然可以清楚地知道本地开放引入的项在哪里使用。相反,使用模块Json.Encode.string的显式限定,语法可能更重,但每个实体的来源更清晰
另一种常用的折衷方法是为常用模块提供短别名:
module Enc = Json.Encode;
Enc.object_([
("type", Enc.string(m.type_)),
("label", Enc.string(m.label))
])
语法M.expr在本地打开表达式expr中的模块M。换句话说,它在表达式中引入模块的所有元素。
例如,在你的情况下
Json.Encode.(object_([("type", string(m.type_)), ("label", string(m.label))]))
转化为
Json.Encode.object_([
("type", Json.Encode.string(m.type_)),
("label", Json.Encode.string(m.label))
])
当处理DSL(如Json.Encoding引入的DSL)时,这种本地开放语法非常有用,同时仍然可以清楚地知道本地开放引入的项在哪里使用。相反,使用模块Json.Encode.string的显式限定,语法可能更重,但每个实体的来源更清晰
另一种常用的折衷方法是为常用模块提供短别名:
module Enc = Json.Encode;
Enc.object_([
("type", Enc.string(m.type_)),
("label", Enc.string(m.label))
])