liveBook · Manning

Type compatibility

As explained in Clojure: The Essential Reference

Type compatibility is the property that two values belong to categories that an operation can compare according to its compatibility rules.

Type compatibility determines whether clojure.data/diff can compare two values structurally. Collections are compatible when both are arrays, sets, lists, or maps; scalar values follow = compatibility rules. section 6.7

When values are compatible, diff returns a three-part result:

  • Index 0 contains values found only in the first argument. section 6.7
  • Index 1 contains values found only in the second argument. section 6.7
  • Index 2 contains shared values; incompatible or unrelated values produce [a b nil]. section 6.7
  • Lists and vectors compare as compatible list types, while a map and a sorted map can trigger ClassCastException. section 6.7

Nested differences

(diff [1 {:a [1 2]}] [1 {:a [1 3]}]) ;; [[nil {:a [nil 2]}] ;; [nil {:a [nil 3]}] ;; [1 {:a [1]}]] Nested compatible collections preserve their structure while separating unique and common values.

How Clojure: The Essential Reference covers it

How deep it goes
Explains collection predicates, scalar equality, nested outputs, exceptions, and deep-recursion limits.
Examples
Uses vectors, lists, maps, sets, arrays, sorted maps, and deeply nested maps as comparison cases.
What you can do after
Diagnose compatible and incompatible comparisons by interpreting diff's unique and common result positions.
How it connects
Connects structural comparison with equality, Clojure data structures, and Java collection types.

Also explained in

Compare how 3 books explain type compatibility →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.