5

I'm at loss of how to create a Java enum in Clojure. I want to create a Java enum that uses a Java interface and then pass it into a Java method, all within Clojure. I want to do this to work with the neo4j graph library (I don't want to use someone's prebuilt interface, I want to write my own interop code).

I searched around the internet, and it looks like I can use the proxy method, but I can't for the life of me get it to work. Here's the equivalent Java code I need to write in Clojure:

private static enum RelTypes implements RelationshipType
{
    KNOWS
}

And here's my stab at it (It's not right :( ):

(proxy [org.neo4j.graphdb.RelationshipType] [] (KNOWS))

I'm also wondering if there's a good website that documents things like this that I'm missing. I know about the Clojure docs on the Clojure site, which is really useful, but for example of usage I can't always find what I need. Perhaps I need a good reference book?

1 Answer 1

8

Why not just create the enum in Java? Sometimes falling back to Java is the simplest answer.

Here is a very old thread about using proxy to define enums from Rich Hickey and Stuart Sierra along with some alternatives using gen-class. I think the proxy path should work with something like this for you:

(proxy [Enum org.neo4j.graphdb.RelationshipType] [ "KNOWS" 1 ])

But that won't generate anything you'd want an external Java user to use, in which case gen-class is likely the better solution.

Sign up to request clarification or add additional context in comments.

1 Comment

And if I'm using gen-class I may as well just use Java... Which is what I did. Thanks for your help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.