Questions tagged [clojure-java-interop]

For topics related to interoperability between Clojure and Java

Programs written in Java and in Clojure run on the JVM and can therefore call each other. Use this tag to mark questions related to this interoperability.

269 questions
176
votes
9 answers

Calling clojure from java

Most of the top google hits for "calling clojure from java" are outdated and recommend using clojure.lang.RT to compile the source code. Could you help with a clear explanation of how to call Clojure from Java assuming you have already built a jar…
Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284
88
votes
2 answers

Why does Clojure have 5 ways to define a class instead of just one?

Clojure has gen-class, reify, proxy and also deftype and defrecord to define new class-like datatypes. For a language that values syntactic simplicity and abhors unnecessary complexity, it seems like an aberration. Could someone explain why it is…
Salil
  • 9,534
  • 9
  • 42
  • 56
47
votes
3 answers

How to handle java variable length arguments in clojure?

I'am wrapping a java lib into clojure, but i have problems dealing with variable length arguments. Say, TestClass.aStaticFunction(Integer... intList){/*....*/} How could i call this function in clojure?
qiuxiafei
  • 5,827
  • 5
  • 30
  • 43
46
votes
3 answers

Java and Clojure with Leiningen

Is it possible to easily manage and compile native Java classes alongside Clojure in a project using leiningen? I am working at a pretty low level (with netty nio) and thinking that some of the plumbing classes would actually be easier to handle as…
Toby Hede
  • 36,755
  • 28
  • 133
  • 162
29
votes
4 answers

Clojure: working with a java.util.HashMap in an idiomatic Clojure fashion

I have a java.util.HashMap object m (a return value from a call to Java code) and I'd like to get a new map with an additional key-value pair. If m were a Clojure map, I could use: (assoc m "key" "value") But trying that on a HashMap…
foxdonut
  • 7,779
  • 7
  • 34
  • 33
20
votes
1 answer

shutdown hook doesn't fire when running with "lein run"

I have the following code: (ns test-hook.core) (defn -main [] (.addShutdownHook (Runtime/getRuntime) (Thread. #(println "shutdown"))) (println "start") (doseq [i (range 1 6)] (Thread/sleep 1000) (println i))) and the following…
Samus_
  • 2,903
  • 1
  • 23
  • 22
16
votes
6 answers

Use Java object as Clojure map

I have a Java class that I'd like to use in Clojure. But, I want to use it as a Clojure map. What are the steps required to do so? I've looked at the code for IPersistentMap -- should the Java class implement that? Or should there be some Clojure…
Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
13
votes
2 answers

Why does (int 10) produce a Long instance?

Why does (int 10) not produce an instance of type java.lang.Integer? ; why Long here? => (type (int 10)) ; java.lang.Long ; this one is also Long, why not java.lang.Number? => (type (num 10)) ; java.lang.Long => (type (double 10)) ;…
Eric Schoonover
  • 47,184
  • 49
  • 157
  • 202
13
votes
4 answers

How can I parameterize access to a Java enum in clojure?

Say I have a Java enum. For example: public enum Suits {CLUBS, DIAMONDS, HEARTS, SPADES}; Normally, I can do something in clojure with that enum like so: (defn do-something [] (let [s Suits/DIAMONDS] (...))) But, I want to write a clojure…
stand
  • 3,054
  • 1
  • 24
  • 27
13
votes
2 answers

Options for creating Java classes in Clojure

There are a few different ways to create Java classes in Clojure, so what are the tradeoffs when picking between gen-class, proxy, and reify in Clojure? (are there other ways to create Java classes that I haven't listed?) My basic understanding is…
pauldoo
  • 18,087
  • 20
  • 94
  • 116
12
votes
0 answers

Are there any issues of including a jar written in clojure as part of an android app?

I'm developing a standard Android app (in java), but would like to implement some of the non-UI "business logic" in clojure. Are there any downsides to doing this, or issues that I should be aware of that would make this a bad idea? I've done a…
Mark Melling
  • 1,562
  • 14
  • 15
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
11
votes
1 answer

Can I partial a Java method invocation in Clojure?

I have a method on an object. myObject.myMethod(1) I can invoke this in Clojure (.myMethod myObject 1) I can also invoke it using information from the lexical environment (let [x 1] (.myMethod myObject x)) Can I do this with a partial? E.g. (let…
Joe
  • 46,419
  • 33
  • 155
  • 245
10
votes
3 answers

Is it possible to use Clojure's case form with a Java enum?

The case doc says Unlike cond and condp, case does a constant-time dispatch... All manner of constant expressions are acceptable in case. I would like to benefit from case's constant-time dispatch to match on Java enums. Java's switch statement…
pron
  • 1,693
  • 18
  • 28
10
votes
2 answers

Clojure deftype: how to constrain field types?

I'm trying to write a Clojure library that can be used from Java without users knowing it is written in Clojure. For this, I need my fields to have proper types: I like that I can do this: (deftype Point [^double x ^double y]) Which generates a…
Frederik
  • 14,156
  • 10
  • 45
  • 53
1
2 3
17 18