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. Because the object is only partly set, its ? is false and its $ is unset: interpolating it into a string prints nothing, so check individual fields (here phoneIsSet()) instead. 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 phoneIsSet() as pure <- rtn as Boolean: phone? 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 set?: ${partial?}`) stdout.println(`Source phone set?: ${partial.phoneIsSet()}`) //Copy transfers everything — including unset phone copy <- UserProfile() copy :=: partial stdout.println(`Copy set?: ${copy?}`) stdout.println(`Copy phone set?: ${copy.phoneIsSet()}`)
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: copy, unset, partial, :=:, fields, tri-state, incomplete