Search code examples
clojurepretty-printdatomic

When coding with Datomic - why does println and pprint treat temporary ids differently?


In Datomic, I can get a temporary ID like following:

(ns demo
  (:require 
    [datomic.api :as d]
    [clojure.pprint :refer :all])))

(d/tempid :db.part/db)

Now when you output this using println you get something similar to:

(println (d/tempid :db.part/db))
;#db/id[:db.part/user -1000003]

Now I understand this is a temporary id.

However when I do the same thing with pretty-print - I get something different:

(pprint (d/tempid :db.part/db))
;{:part :db.part/user, :idx -1000002}

Why is this different? Is there something fundamental about temporary IDs I'm missing?

Assumptions:

  • Here are my dependencies in Leiningen:

    :dependencies [[org.clojure/clojure "1.6.0"] [com.datomic/datomic-free "0.9.5130"]]

My question is: When coding with Datomic - why does println and pprint treat temporary ids differently?


Solution

  • It looks like d/tempid returns either a record or an object with a custom toString method. While println prints records with their name, pprint prints them as plain maps. Try this on the REPL:

    user=> (defrecord MyRecord [a])
    user.MyRecord
    user=> (def m (My. 1))
    #'user/m
    user=> (println m)
    #user.My{:a 1}
    nil
    user=> (clojure.pprint/pprint m)
    {:a 1}
    nil
    

    #user.My{:a 1} doesn't look exactly like #db/id[:db.part/user -1000003] but you can see the difference between using pprint and println for things like records or other objects.

    Edit: (type (d/tempid :db.part/user)) returns datomic.db.DbId which is a custom type.