【问题标题】:What is this syntax: template tplname{op(id,[id2])}(params)这是什么语法:模板 tplname{op(id,[id2])}(params)
【发布时间】:2018-04-02 13:50:57
【问题描述】:
在json模块中:
template simpleGetOrDefault*{`{}`(node, [key])}(node: JsonNode, key: string): JsonNode = node.getOrDefault(key)
花括号是怎么回事(它们里面是什么)?
【问题讨论】:
标签:
templates
syntax
operator-overloading
curly-braces
nim-lang
【解决方案1】:
这是"term-rewriting macro" 的示例。
在 json 模块的前面一点,您会发现 {} 运算符的定义具有以下签名:
proc `{}`*(node: JsonNode, keys: varargs[string]): JsonNode =
## Traverses the node and gets the given value. If any of the
## keys do not exist, returns ``nil``. Also returns ``nil`` if one of the
## intermediate data structures is not an object.
术语重写宏的目标是拦截仅将单个字符串作为参数提供给运算符的情况,并将其转换为对getOrDefault 的简单调用。