leveraging Clojure STM in other JVM languages?

108 views
Skip to first unread message

Raoul Duke

unread,
May 1, 2009, 1:40:07 PM5/1/09
to clo...@googlegroups.com
hi,

has anybody experimented with using Clojure code from e.g. Scala, to
get Clojure STM goodness in other languages?

thanks.

Stuart Sierra

unread,
May 1, 2009, 4:21:49 PM5/1/09
to Clojure
Have not tried it myself, but the (gen-class :state ...) form is
supposed to help with this.
-SS

Rich Hickey

unread,
May 2, 2009, 10:34:07 AM5/2/09
to Clojure


On May 1, 1:40 pm, Raoul Duke <rao...@gmail.com> wrote:
The Clojure STM is just a Java library. You could use clojure.lang.Ref
and LockingTransaction directly from Java/Scala/etc.

runInTransaction takes a Callable, and the values placed in Refs can
be anything immutable.

Rich

wilfred

unread,
Jun 22, 2009, 3:18:20 PM6/22/09
to Clojure
So Raoul, did you give it a try after all of this?

Wilson MacGyver

unread,
Jun 22, 2009, 5:45:10 PM6/22/09
to clo...@googlegroups.com
that seems very hard to do. How would you grantee no side effect from other languages?
--
Omnem crede diem tibi diluxisse supremum.

Raoul Duke

unread,
Jun 22, 2009, 5:48:26 PM6/22/09
to clo...@googlegroups.com
> So Raoul, did you give it a try after all of this?

in a word... no.

:-}

Daniel Lyons

unread,
Jun 22, 2009, 6:07:35 PM6/22/09
to clo...@googlegroups.com
On Jun 22, 2009, at 3:45 PM, Wilson MacGyver wrote:

that seems very hard to do. 

I wrote some code that called Clojure from Jython just to see if it would work. All you have to do is use :gen-class to compile it and call it as though it's Java. I haven't used Scala so I'm not sure what its like calling Java from Scala but it shouldn't be any different than calling methods on any other Java object. I don't think this can interfere with Clojure's concurrency model, since it's implemented atop Java's concurrency primitives.

How would you grantee no side effect from other languages?

Not sure you have to. Clojure's variables/refs/atoms/agents/etc. do not expose their contents in a writeable way without going through their APIs. I'm pretty sure Clojure doesn't mind if calling external code results in side effects on the other side, and stuff on the other side can't manipulate Clojure's immutable types as though they are mutable due to Java's access protection mechanism. You would have to worry about side-effects happening on the other side during Clojure transactions that got replayed though.

— 
Daniel Lyons

MattH

unread,
Jun 24, 2009, 12:29:01 AM6/24/09
to Clojure
I'm calling the clojure.lang.Ref and LockingTransaction classes
directly from Java (as suggested by Rich above).

Yes, as Daniel says you to need to segregate side effects from the
code that runs in a transaction. I'm using some lightweight wrappers
around Ref and LockingTransaction to store a queue of side effects,
all of which get run on commit of the outer-most transaction (similar
to the use of agents, which aren't exposed in the Java classes IIRC).

The approach works well in practice, minor pain points so far have
been Java's handling of checked exceptions (which doesn't fit well
with functional programming), and selecting the right library of
immutable data structures to work with from Java. Google collections
have a fantastic API and support generics (but aren't persistent so
you have to do copy on write); Clojure's data structures are
wonderful, but don't use generics. pcollections[1] appears to fill the
gap, but not sure what state that project is in.

[1] http://code.google.com/p/pcollections/
Reply all
Reply to author
Forward
0 new messages