Clojure

protocol-ize with-open

Details

  • Type: Enhancement Enhancement
  • Status: Open Open
  • Priority: Minor Minor
  • Resolution: Unresolved
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
  • Patch:
    Code
  • Approval:
    Triaged

Description

Good use (and documentation example) of protocols: make with-open aware of a Closable protocol for APIs that use a different close convention. See http://groups.google.com/group/clojure/browse_thread/thread/86c87e1fc4b1347c

Activity

Hide
Tassilo Horn added a comment -

Hi Brandon, two questions:

Could 1) be circumvented somehow by providing a default implementation somehow? I guess the protocol could be extended upon Object with implementation (.close this), but that would give a reflection warning since Object has no close method. Probably one could extend upon Object and in the implementation search a "close" method using java.lang.reflect and throw an exception if none could be found?

Could you please tell me a bit more about the :on-interface option? How does that differ from extend? And how do I add the implementation, i.e., (.close this) with that option?

Show
Tassilo Horn added a comment - Hi Brandon, two questions: Could 1) be circumvented somehow by providing a default implementation somehow? I guess the protocol could be extended upon Object with implementation (.close this), but that would give a reflection warning since Object has no close method. Probably one could extend upon Object and in the implementation search a "close" method using java.lang.reflect and throw an exception if none could be found? Could you please tell me a bit more about the :on-interface option? How does that differ from extend? And how do I add the implementation, i.e., (.close this) with that option?
Hide
Brandon Bloom added a comment -

I looked up this ticket because I ran in to a reflection warning: with-open does not hint it's binding with java.io.Closeable

Some feedback on the patch:

1) This is a breaking change for anyone relying on the close method to be duck-typed.

2) CloseableResource is a bit long. clojure.core.protocols.Closeable is plenty unambiguous.

3) Rather than extending CloseableResource to java.io.Closeable, you can use the little known (undocumented? unsupported?) :on-interface directive:

(defprotocol Closeable
  :on-interface java.io.Closeable
  (close [this]))

That would perform much better than the existing patch.

Show
Brandon Bloom added a comment - I looked up this ticket because I ran in to a reflection warning: with-open does not hint it's binding with java.io.Closeable Some feedback on the patch: 1) This is a breaking change for anyone relying on the close method to be duck-typed. 2) CloseableResource is a bit long. clojure.core.protocols.Closeable is plenty unambiguous. 3) Rather than extending CloseableResource to java.io.Closeable, you can use the little known (undocumented? unsupported?) :on-interface directive:
(defprotocol Closeable
  :on-interface java.io.Closeable
  (close [this]))
That would perform much better than the existing patch.
Hide
Tassilo Horn added a comment -

Updated patch to apply cleanly against master again.

Show
Tassilo Horn added a comment - Updated patch to apply cleanly against master again.
Hide
Andy Fingerhut added a comment -

Patch 0001-Added-ClosableResource-protocol-for-with-open.patch dated 08/Mar/12 applies, builds, and tests cleanly on latest master as of Apr 2 2012. Tassilo has signed a CA.

Show
Andy Fingerhut added a comment - Patch 0001-Added-ClosableResource-protocol-for-with-open.patch dated 08/Mar/12 applies, builds, and tests cleanly on latest master as of Apr 2 2012. Tassilo has signed a CA.
Hide
Tassilo Horn added a comment -

Updated patch.

Show
Tassilo Horn added a comment - Updated patch.
Hide
Tassilo Horn added a comment -

Seems to be related to Scopes (http://dev.clojure.org/jira/browse/CLJ-2).

Show
Tassilo Horn added a comment - Seems to be related to Scopes (http://dev.clojure.org/jira/browse/CLJ-2).
Hide
Tassilo Horn added a comment -

Added a CloseableResource protocol and extended it on java.io.Closeable (implemented by all Readers, Writers, Streams, Channels, Sockets). Use it in with-open.

All tests pass.

Show
Tassilo Horn added a comment - Added a CloseableResource protocol and extended it on java.io.Closeable (implemented by all Readers, Writers, Streams, Channels, Sockets). Use it in with-open. All tests pass.
Hide
Assembla Importer added a comment -
Show
Assembla Importer added a comment - Converted from http://www.assembla.com/spaces/clojure/tickets/308

People

Vote (7)
Watch (4)

Dates

  • Created:
    Updated: