Clojure import/require/use defrecord from another namespace

Good GOD!!
Mixing Lisp and Java leads to weirdness of infinite magnitude when I just spent 1 hour trying to figure out wtf was happening with a compilation error. Here’s the situation.
– I created a package called building_blocks (directory building_blocks).
– Namespace called building-blocks.activity (activity.clj file is under building_blocks directory)
– I have a defrecord called Activity inside the building-blocks.activity namespace
– I wanted to use the above mentioned Activity defrecord inside another namespace called adder.core

I continuously got “java.lang.ClassNotFoundException”, “exception in thread “main” java.lang.IllegalArgumentException: Unable to resolve classname: Activity, compiling:(adder/core.clj ” and a bunch of other errors.

As it turns out there were two errors:
– Importing defrecord from another namespace is not just :use. I had to first :require the namespace, then import the defrecord. This was a trivial problem to solve and I figured it out quickly. Only this did not work in my case
– Dashes “-” and Underscores “_” are a nuisance since we are mixing Lisp with Java. While the file system uses underscores Clojure converts things to dashes. Brilliant.

So to fix the second error I use the follow in the ns block

(ns adder.core
(:require building-blocks.activity)
(:import [building_blocks.activity Activity])
)

then I do
(def new-activity (Activity. name))

Advertisements

One response to “Clojure import/require/use defrecord from another namespace

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s