Using statement
The using statement provides syntactic convenience in modules where the same parameter names and types are used over and over. Instead of:
proc foo(c: Context; n: Node) = ...
proc bar(c: Context; n: Node, counter: int) = ...
proc baz(c: Context; n: Node) = ...
One can tell the compiler about the convention that a parameter of name c should default to type Context, n should default to Node etc.:
using
c: Context
n: Node
counter: int
proc foo(c, n) = ...
proc bar(c, n, counter) = ...
proc baz(c, n) = ...
proc mixedMode(c, n; x, y: int) =
# 'c' is inferred to be of the type 'Context'
# 'n' is inferred to be of the type 'Node'
# But 'x' and 'y' are of type 'int'.
The using section uses the same indentation based grouping syntax as a var or let section.
Note that using is not applied for template since the untyped template parameters default to the type system.untyped.
Mixing parameters that should use the using declaration with parameters that are explicitly typed is possible and requires a semicolon between them.
当前内容版权归 nim-lang.org 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 nim-lang.org .