4

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...

1 Answer 1

5

Instead of

(deftype NewsItem
  [text date]
  Saving)

Just use:

(deftype NewsItem
  [text date])
Sign up to request clarification or add additional context in comments.

1 Comment

The reason for this is that by including Saving you're telling Clojure that NewsItem has its own implementation of save, but it doesn't.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.