Check if two Config records have identical values.

← Operators and Expressions · Ref: Q1083

Check equality and inequality with == and <>:

  if primary == replica
    stdout.println("match")
  if primary <> staging
    stdout.println("differ")

'default operator' generates == and <> from the <=> comparison. Both objects must be SET for meaningful comparison.

See Q1082 for ordering comparison.

Example

defines module qa.operators.recordequality

  defines class

    Config
      host <- String()
      port <- Integer()

      Config()
        ->
          host as String
          port as Integer
        this.host :=: host
        this.port :=: port

      default operator

  defines program

    RecordEqualityDemo()
      stdout <- Stdout()

      primary <- Config("db.prod", 5432)
      replica <- Config("db.prod", 5432)
      staging <- Config("db.stage", 5432)

      //Equal values
      if primary == replica
        stdout.println("Primary and replica match")

      //Different values
      if primary <> staging
        stdout.println("Primary and staging differ")

Common mistakes

E50060 — EK9 has no .equals() method; use the == operator for equality and <> for inequality. See ek9 -h E50060 for details.

Incorrect:

if primary.equals(replica)

Correct:

if primary == replica
Other ways to ask this
  • Test equality between two objects using ==
  • Determine if two records contain the same data
  • In Java I'd use .equals(). How do I check equality in EK9?
  • I have two config objects and need to verify they match

Coming from another language?

Java: .equals() and Objects.equals(). Python: == (with __eq__). Rust: PartialEq. Go: reflect.DeepEqual or manual. EK9: default operator == or custom operator ==.

Keywords: ==, compare, <>, equal, match, identical, equality