So I have come up with a solution to my desire to move to defprotocol/
type in spite of my requirement for gen-class' init/post-init
methods :

(def protocol Resource
 (open [this])
 (close [this])
...
)

(deftype ResourceImpl [resource]
 Resource
 (open [this]...)
 (close [this] (.close resource)...)
....)

(defn init-resource-impl [path]
 (ResourceImpl (acquire-resource path)))

Use an aux fn as a ctor/init (init-resource-impl) - this gives you the
opportunity to take a few params from upstream and acquire necessary
resources to inject into your new types ctor. The contract between
init-fn and type is that the type takes ownership of all resources
acquired within the fn and is responsible for tidying them up when
they are finished with.

Post-init fn-ality (which needs a self-reference) may be done in a
method - in this case open(). If you are in the unfortunate
circumstance where post-init also needs to mutate your instance's
state, then you will have to leave an atom placeholder in the type def
and inject an atom in your init fn.

I can tidy up using another method - close() - if this needs to mutate
state then you will need to hold this in an atom as above.

The advantage (for me) of doing this over using gen-class is that my
code is much simpler to write as I do not have to manage all state
explicitly anymore.

I'm having a little trouble type-hinting etc but am hoping that I can
figure it all out.

Hope this helps anyone who finds themselves in the same position as
myself :-)


Jules


On Mar 25, 10:22 am, Jules <jules.gosn...@gmail.com> wrote:
> yes
>
> and that's great where the resource usage is scoped on a per-thread basis,
> but not a per-object basis - but then, I am thinking in OO terms again :-)
>
> Jules

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to