Object construction
Objects can also be created with an object construction expression that has the syntax T(fieldA: valueA, fieldB: valueB, …) where T is an object type or a ref object type:
type
Student = object
name: string
age: int
PStudent = ref Student
var a1 = Student(name: "Anton", age: 5)
var a2 = PStudent(name: "Anton", age: 5)
# this also works directly:
var a3 = (ref Student)(name: "Anton", age: 5)
# not all fields need to be mentioned, and they can be mentioned out of order:
var a4 = Student(age: 5)
Note that, unlike tuples, objects require the field names along with their values. For a ref object type system.new is invoked implicitly.
当前内容版权归 nim-lang.org 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 nim-lang.org .