sirthias / pegdown

A pure-Java Markdown processor based on a parboiled PEG parser supporting a number of extensions
http://pegdown.org
Apache License 2.0
1.29k stars 217 forks source link

Create Interface for LinkRenderer for Better Clojure Interop #132

Open espeed opened 10 years ago

espeed commented 10 years ago

Clojure/Java interop works best with Java interfaces. Creating an interface for LinkRenderer would open up the interop options and increase performance for custom Clojure LinkedRenderer implementations.

https://github.com/sirthias/pegdown/blob/master/src/main/java/org/pegdown/LinkRenderer.java

Regarding Clojure/Java interop and interfaces (from http://clojure.org/datatypes)...

Datatypes and protocols are opinionated

While datatypes and protocols have well-defined relationships with host constructs, and make for a great way to expose Clojure functionality to Java programs, they are not primarily interop constructs. That is, they make no effort to completely mimic or adapt to all of the OO mechanisms of the host. In particular, they reflect the following opinions:

  • Concrete derivation is bad \ you cannot derive datatypes from concrete classes, only interfaces
  • You should always program to protocols or interfaces \ datatypes cannot expose methods not in their protocols or interfaces
  • Immutability should be the default \ and is the only option for records
  • Encapsulation of information is folly \ fields are public, use protocols/interfaces to avoid dependencies
  • Tying polymorphism to inheritance is bad \ protocols free you from that

If you use datatypes and protocols you will have a clean, interface-based API to offer your Java consumers. If you are dealing with a clean, interface-based Java API, datatypes and protocols can be used to interoperate with and extend it. If you have a 'bad' Java API, you will have to use gen-class. Only in this way can the programming constructs you use to design and implement your Clojure programs be free of the incidental complexities of OO.