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:

  1. type
  2. Student = object
  3. name: string
  4. age: int
  5. PStudent = ref Student
  6. var a1 = Student(name: "Anton", age: 5)
  7. var a2 = PStudent(name: "Anton", age: 5)
  8. # this also works directly:
  9. var a3 = (ref Student)(name: "Anton", age: 5)
  10. # not all fields need to be mentioned, and they can be mentioned out of order:
  11. 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.