Hi all,
If you have a protocol
(defprotocol Foo (foo [x] [x y]))
and implement it for the record
(defrecord Bar [p q r]
Foo
(foo [x] x)
(foo [x y] [x y]))
you write each method separately. The same is true for extend-type
. Yet, if you use extend-protocol
syntax is more like usual Clojure multi-arity function definition:
(extend-protocol Foo
Bar
(foo
([x] x)
([x y] [x y])))
The first question is why there is a difference?
Secondly, if you mess up which syntax is where, the error you will get is quite obscure, nothing guards you against essentially a wrong syntax: both extend-type
and extend-protocol
are macros, so it shouldn’t be too difficult to add a quick check on correctness.
But lastly, wouldn’t it be better to have a uniform syntax?
Regards,
Alexey
Hi Tassilo,
Thanks for the reply.
The thing is, although the implementations for extending protocols directly in records and with extend-type
might be different, there is no reason why the syntax should be different: from Clojure-programmer perspective it’s all the same. It is a leaked abstraction if we have to thing about those details. The same applies for interface implementation: overloaded methods there and multi-arity functions in Clojure shouldn’t look differently to a Clojure programmer (unless there is some more profound difference).