Questions tagged [clojure-protocol]

Protocols were introduced in Clojure 1.2

Protocols were introduced in Clojure 1.2.

Clojure is written in terms of abstractions. There are abstractions for sequences, collections, callability, etc. In addition, Clojure supplies many implementations of these abstractions. The abstractions are specified by host interfaces, and the implementations by host classes. While this was sufficient for bootstrapping the language, it left Clojure without similar abstraction and low-level implementation facilities. The protocols and datatypes features add powerful and flexible mechanisms for abstraction and data structure definition with no compromises vs the facilities of the host platform.

There are several motivations for protocols:

  • Provide a high-performance, dynamic polymorphism construct as an alternative to interfaces
  • Support the best parts of interfaces
    ° specification only, no implementation
    ° a single type can implement multiple protocols
  • While avoiding some of the drawbacks
    ° Which interfaces are implemented is a design-time choice of the type author, cannot be extended later (although interface injection might eventually address this)
    ° implementing an interface creates an isa/instanceof type relationship and hierarchy
  • Avoid the 'expression problem' by allowing independent extension of the set of types, protocols, and implementations of protocols on types, by different parties
    ° do so without wrappers/adapters
  • Support the 90% case of multimethods (single dispatch on type) while providing higher-level abstraction/organization

Reference: http://clojure.org/protocols

8 questions
12
votes
1 answer

Creating a library of Protocols and defrecords for use from Java

At the moment, I have a completely functional Clojure library which is called from Java. The way I do this : I have a file that uses gen-class to wrap the entire API as static methods of a single class and passes data in and out in the form of…
interstar
  • 26,048
  • 36
  • 112
  • 180
10
votes
1 answer

Is there a Clojure compile-time tool to check if a record or type actually implements a protocol it claims to?

Seems the Clojure compiler doesn't do this by default : Does the Clojure compiler check if records and types implement protocols? Any, say, Lein plugins that do this?
interstar
  • 26,048
  • 36
  • 112
  • 180
9
votes
1 answer

Clojure - dispatch on return type? (As expressive as Haskell Typeclasses)

This is a question about the expressiveness of Clojure vs other languages such as Haskell. The broader issue is solutions to the Expression Problem This question reached the conclusion that in general Clojure protocols (and multimethods) were less…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
3
votes
1 answer

Precedence of clojure protocols in another namespace

In a project using clojure.java.jmx, I was extending it's Destract protocols objects->data function to transform more of the JMX data structures returned from calls or metadata queries into plain clojure data structures. When I was done with the…
NielsK
  • 6,886
  • 1
  • 24
  • 46
3
votes
0 answers

How to implement an interface via protocols?

Using extend-protocol, a protocol P can provide a default implementation for anything that implements interface I. This essentially teaches I's to do new things. If we want a type or record to provide the functionality of I we still need to extend…
muhuk
  • 15,777
  • 9
  • 59
  • 98
1
vote
1 answer

Does the Clojure compiler check if records and types implement protocols?

Is the Clojure compiler meant to check if a record or type that says it instantiates a protocol actually implements the methods listed in it? I'm trying this out now and so far, it doesn't seem to.
interstar
  • 26,048
  • 36
  • 112
  • 180
0
votes
1 answer

Sealed Clojure protocols

I'd like to know if Clojure utilizes the sealed interface/ implementing record pattern in Java, and if so how to define it. I was thinking along the lines of something like: (defprotocol ;; protocol definition here :allows …
0
votes
1 answer

Clojure deftype and protocol

I have this Clojure code in my (ns handlers) (defprotocol ActionHandler (handle [params session])) (defrecord Response [status headers body]) (deftype AHandler [] ActionHandler (handle [params session] (Response. 200…
greggigon
  • 3
  • 2