Copy a component and check if the copy equals the original.
← Dependency Injection · Ref: Q1106
Copy and compare components using :=: and ==:
copy <- Logger() copy :=: original if copy == original stdout.println("Match")
'default operator' generates :=: and == from component fields, same as for classes.
See Q1105 for defining components. See Q1075 for :=: copy details.
Example
defines module qa.di.componentcopyequality defines component Config host <- "localhost" port <- 8080 getHost() <- rtn as String: host getPort() <- rtn as Integer: port default operator defines program ComponentCopyEqualityDemo() stdout <- Stdout() original <- Config() stdout.println(`Original: ${original}`) //Copy backup <- Config() backup :=: original stdout.println(`Copy: ${backup}`) //Equality if original == backup stdout.println("Original and copy are equal")
Common mistakes
E50060 — EK9 has no .clone() method; use ':=:' to copy a component's fields. See ek9 -h E50060 for details.
Incorrect:
backup := original.clone()
Correct:
backup :=: original
Other ways to ask this
- I need to duplicate a component and verify the copy matches
- In Java I'd clone a service bean. Write the EK9 component copy
- Given a Logger component, create a copy using :=: and test equality with ==
- Duplicate a component's state into a new instance
Coming from another language?
Java: no standard bean cloning. Spring: prototype scope creates new instances. EK9: :=: copies component field values directly.
Keywords: copy, :=:, component, ==, duplicate, equality