An abstract IdRecord and a CustomerDetail record with copy, merge and fuzzy match

The complete source of records_customers.ek9, compiled by the EK9 compiler. Shown on records.html.

#!ek9
defines module site.records.customers
  defines type

    Index as Integer constrain as
      > 0

  defines function
    defaultDateOfBirth()
      <- rtn as Date: Date()

    //IdRecord's '<=>' (by id) is the sameness of the whole hierarchy; a subtype cannot redefine it.
    //Ordering customers by their details is therefore a comparator function, not a second '<=>'.
    compareByBirthThenName()
      ->
        customer1 as CustomerDetail
        customer2 as CustomerDetail
      <- rtn as Integer: customer1.dateOfBirth <=> customer2.dateOfBirth

      if rtn == 0
        rtn: customer1.lastName <=> customer2.lastName
      if rtn == 0
        rtn: customer1.firstName <=> customer2.firstName

  defines record

    IdRecord as abstract
      //We can support simple inference, but not complex expressions for fields/properties.
      id <- Index()
      createdAt as DateTime: SystemClock().dateTime()

      IdRecord()
        -> id as Index
        require id?
        this.id: id

      operator $ as pure
        <- rtn as String: `${$id} ${$createdAt}`
      override operator ? as pure
        <- rtn as Boolean: id?
      //The sameness of every IdRecord: ==, <>, <, <=, > and >= are generated from it
      operator <=> as pure
        -> item as IdRecord
        <- rtn as Integer: id <=> item.id
      operator #? as pure
        <- rtn as Integer: #? id

    CustomerDetail extends IdRecord
      firstName as String: String()
      lastName as String: String()
      dateOfBirth as Date: defaultDateOfBirth()

      CustomerDetail()
        ->
          id as Index
          firstName as String
          lastName as String

        super(id)
        require firstName?
        require lastName?
        this.firstName: firstName
        this.lastName: lastName

      CustomerDetail()
        ->
          id as Index
          dateOfBirth as Date

        super(id)
        require dateOfBirth?
        this.dateOfBirth: dateOfBirth

      CustomerDetail()
        ->
          id as Index
          firstName as String
          lastName as String
          dateOfBirth as Date

        super(id)
        require firstName?
        require lastName?
        require dateOfBirth?
        this.firstName: firstName
        this.lastName: lastName
        this.dateOfBirth: dateOfBirth

      operator :=:
        -> item as CustomerDetail
        id :=: item.id
        firstName :=: item.firstName
        lastName :=: item.lastName
        dateOfBirth :=: item.dateOfBirth

      operator :~:
        -> item as CustomerDetail
        if not id?
          id :=: item.id
        if not firstName?
          firstName :=: item.firstName
        if not lastName?
          lastName :=: item.lastName
        if not dateOfBirth?
          dateOfBirth :=: item.dateOfBirth

      operator <~> as pure
        -> item as CustomerDetail
        //Use a fuzzy match on string version of date of birth first
        <- rtn as Integer: ($this.dateOfBirth <~> $item.dateOfBirth) +
        (this.lastName <~> item.lastName) +
        (this.firstName <~> item.firstName)

      operator #^ as pure
        <- rtn as String: $this

      override operator $ as pure
        <- rtn as String: `${super.$()} ${firstName} ${lastName} ${$dateOfBirth}`
      override operator ? as pure
        <- rtn as Boolean: super.?() and firstName? and lastName? and dateOfBirth?

  defines program
    ShowCustomerRecords()
      stdout <- Stdout()

      unknownCustomer <- CustomerDetail()
      stdout.println(`A customer with no details is set: ${unknownCustomer?}`)

      customer1 <- CustomerDetail(id: Index(1), firstName: "Gomez", lastName: "Addams", dateOfBirth: 1963-06-08)
      customer2 <- CustomerDetail(id: Index(2), firstName: "Morticia", lastName: "Addams", dateOfBirth: 1965-01-03)
      customer3 <- CustomerDetail(id: Index(3), firstName: "Pugsley", lastName: "Addams", dateOfBirth: 1984-10-21)

      //'<>' is generated from IdRecord's '<=>', so two customers differ by id.
      stdout.println(`Gomez and Morticia are different records: ${customer1 <> customer2}`)

      stdout.println(`Three Addams, ${customer1.firstName} ${customer2.firstName} and ${customer3.firstName}`)
      //Some Addams we've not see before but born on same day or typo of Gomez?
      customer4 <- CustomerDetail(id: Index(4), firstName: "Goetez", lastName: "Addams", dateOfBirth: 1963-06-08)
      comp1 <- compareByBirthThenName(customer4, customer1)
      comp2 <- compareByBirthThenName(customer4, customer2)
      comp3 <- compareByBirthThenName(customer4, customer3)
      stdout.println(`Compares [${comp1} ${comp2} ${comp3}]`)

      best <- customer4 <~> customer1 < customer4 <~> customer2 <- customer1 else customer2
      best: customer4 <~> customer3 < customer4 <~> best <- customer3 else best
      stdout.println(`The best match for ${customer4.firstName} is ${best.firstName}`)

      //full copy of the details
      unknownCustomer :=: best
      //now while objects differ contents are the same.
      stdout.println(`After the copy, compared with Gomez: ${compareByBirthThenName(unknownCustomer, customer1)}`)

      partialCustomer1 <- CustomerDetail(Index(1), "Gomez", "Addams")
      partialCustomer2 <- CustomerDetail(Index(1), 1963-06-08)
      //As both are partial the result of comparing them is unset!
      stdout.println(`Two partial customers compare: ${compareByBirthThenName(partialCustomer1, partialCustomer2)?}`)

      partialCustomer1 :~: partialCustomer2
      //After merging the two should match Gomez.
      stdout.println(`After the merge, compared with Gomez: ${compareByBirthThenName(partialCustomer1, customer1)}`)

Download records_customers.ek9

Output

A customer with no details is set: false
Gomez and Morticia are different records: true
Three Addams, Gomez Morticia and Pugsley
Compares [-1 -1 -1]
The best match for Goetez is Gomez
After the copy, compared with Gomez: 0
Two partial customers compare: false
After the merge, compared with Gomez: 0