Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure - default protocol implementation on Object fails?

I have created a simpe protocol and a type in Clojure:

(defprotocol Saving
  (save [this] "saves to mongodb"))

;default implementation
(extend-type Object
  Saving
  (save [this] (encode this)))


(deftype NewsItem
  [text date]
  Saving)

However, when I try:

=> (def news-item (->NewsItem "Super News!!!" "today"))

and then:

=> (save news-item)

I get:

AbstractMethodError luminous_test.models.model.NewsItem.save()Ljava/lang/Object;  luminous-test.models.model/eval2450 (NO_SOURCE_FILE:1)

What am I doing wrong? I feel like following the lines of creating a default protocol implementation but that's what I am getting...

like image 977
noncom Avatar asked Oct 17 '25 22:10

noncom


1 Answers

Instead of

(deftype NewsItem
  [text date]
  Saving)

Just use:

(deftype NewsItem
  [text date])
like image 190
Ankur Avatar answered Oct 20 '25 18:10

Ankur