Nim 语言是否有更短的对象初始化符号?
Does Nim language has shorter notation for object initialization?
type
BinaryTree*[T] = ref object # BinaryTree is a generic type with
# generic param ``T``
le, ri: BinaryTree[T] # left and right subtrees; may be nil
data: T # the data stored in a node
proc newNode*[T](data: T): BinaryTree[T] =
# constructor for a node
new(result)
result.data = data
是否可以使用像
这样的单行快捷方式
proc newNode*[T](data: T): BinaryTree[T] =
data.new(data = data)
The tutorial says: "Note that referential data types will be nil at the start of the procedure, and thus may require manual initialisation" and here “要分配新的跟踪对象,必须使用 built-in 新 过程 ”。但是如果你真的需要保存那一行,你可以做一个模板:
template aNewNode(data: untyped): void =
new(result)
result.data = data
proc newNode*[T](data: T): BinaryTree[T] =
# constructor for a node
aNewNode data
type
BinaryTree*[T] = ref object # BinaryTree is a generic type with
# generic param ``T``
le, ri: BinaryTree[T] # left and right subtrees; may be nil
data: T # the data stored in a node
proc newNode*[T](data: T): BinaryTree[T] =
# constructor for a node
new(result)
result.data = data
是否可以使用像
这样的单行快捷方式proc newNode*[T](data: T): BinaryTree[T] =
data.new(data = data)
The tutorial says: "Note that referential data types will be nil at the start of the procedure, and thus may require manual initialisation" and here “要分配新的跟踪对象,必须使用 built-in 新 过程 ”。但是如果你真的需要保存那一行,你可以做一个模板:
template aNewNode(data: untyped): void =
new(result)
result.data = data
proc newNode*[T](data: T): BinaryTree[T] =
# constructor for a node
aNewNode data