E02010: Property/Field duplicated
What went wrong
A property/field with this name already exists in the type hierarchy. Child types cannot redeclare properties that exist in parent types - records, classes, components and dynamic classes alike, anywhere up the super chain, whatever the type.
Worked examples
Compilable examples demonstrating this error and the correct fix:
- ek9 -q 819 (Why does EK9 reject duplicate property fields in records?)
- ek9 -q 829 (Why does EK9 reject duplicate properties in records with ...)
How to fix it
- Set the inherited property instead of redeclaring it (in the constructor, or through the parent's constructor or methods)
- Use a different property name if it really is different state
- If different type needed, reconsider your type hierarchy design
Rationale
Properties are inherited in EK9. Redeclaring creates ambiguity:
- Which 'prop3' should be used when accessed?
- JSON serialization would have duplicate keys
- Assignment becomes unpredictable
Class and component properties are private, but a redeclared one is still a SECOND field: the parent's methods read the parent's copy and the child's methods read the child's copy, so one piece of state silently splits in two. EK9 rejects it rather than allow that.
Common causes
- Child class or component declares property with same name as parent
- Redeclaring a property to give it a different initial value
- Record extends another and redeclares a field
- Forgetting that properties are inherited
See also
Examples
Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.
Fails with E02010: Property/Field duplicated
#!ek9 defines module error.e02010.bad defines record RBase1 as open prop1 <- String() prop3 <- Date() default operator ? RExtension1 extends RBase1 prop3 <- String() default operator ? RExtension2 extends RBase1 prop1 <- String() prop3 <- Time() default operator ?
Fixed: An extension adds properties of its own; it never redeclares an inherited one
#!ek9 defines module error.e02010.good defines record RBase1 as open prop1 <- String() prop3 <- Date() default operator ? RExtension1 extends RBase1 prop4 <- String() default operator ? RExtension2 extends RBase1 prop5 <- Time() default operator ?
Questions and answers
These Q&A entries show E02010 and its fix: