Copy a record that has some fields unset to another record.

← Operators and Expressions · Ref: Q1076

:=: copies ALL fields including unset ones:

  partial <- UserProfile("Alice", "alice@example.com")
  copy <- UserProfile()
  copy :=: partial
  stdout.println(`Copy set?: ${copy?}`)

Phone stays unset in the copy — :=: is a faithful snapshot. To copy only SET fields, use :~: merge instead.

See Q1075 for full copy. See Q1077 for merge. See Q1058 for overview.

Example

defines module qa.operators.copypartialrecord

  defines class

    UserProfile
      name <- String()
      email <- String()
      phone <- String()

      UserProfile()
        ->
          name as String
          email as String
        this.name :=: name
        this.email :=: email
        //phone remains unset

      default operator ?
      default operator $
      default operator :=:

  defines program

    CopyPartialRecordDemo()
      stdout <- Stdout()

      //Only name and email are set, phone is unset
      partial <- UserProfile("Alice", "alice@example.com")
      stdout.println(`Source: ${partial}`)
      stdout.println(`Source set?: ${partial?}`)

      //Copy transfers everything — including unset phone
      copy <- UserProfile()
      copy :=: partial
      stdout.println(`Copy: ${copy}`)
      stdout.println(`Copy set?: ${copy?}`)
Other ways to ask this
  • I have a user profile with only name and email filled in and need to duplicate it
  • In Java clone() copies all fields including nulls. Does EK9 :=: copy unset fields too?
  • Given a partially-initialised config, create an exact copy preserving the unset state
  • Transfer partial data between two objects using :=:

Coming from another language?

Java: no equivalent — clone() copies all fields. Python: copy.copy() copies all. EK9 distinguishes :=: (copy all including unset) from :~: (merge only set fields).

Keywords: partial, unset, copy, incomplete, tri-state, fields, :=: