DEV Community

Lance Bachmeier
Lance Bachmeier

Posted on

Messing with ClojureScript

My usage of Javascript has been limited to copying and pasting a few code snippets from the web. I don't do much web development, and when I do, I normally use CGI with D. JS does offer some big advantages, however, like not requiring a running web server.

The main reason I haven't learned JS is that I don't like it. That's not due to flaws in the language - though like any language, those exist - it's purely a matter of preference.

I worked with Clojure years ago, but didn't really have a good use for it, so I set it aside and never returned. ClojureScript has some appeal because it allows me to write Clojure rather than JS. So last night I checked it out.

I went to the website and found a "Quick Start" page. The first thing I learned is that this isn't really true:

If you are using macOS or Linux the only dependencies required for the main part of this tutorial are a web browser and an installation of Clojure.

You actually need an installation of Clojure as described at that link. It doesn't work if you install Clojure using your distribution's package manager.

I then created several directories and the two files. I was left guessing which was the directory to do the compilation, so I tried the one with my source file (that seemed natural coming from the C world). Nope. I had to go to the top directory and do it from there.

After a while, a browser tab opened and I waited for something to happen. Nothing happened. For some reason, an html file that does nothing gets opened. I returned to the terminal. I could type things in, but this was in no way different from just running a Clojure repl. At that point, I was done with my "quick start". I didn't feel I got started on much of anything, because I hadn't replaced JS with Clojure (the whole reason I tried ClojureScript). So I decided to look around for a guide that might show me what to do. There are a lot of links, but there was really nothing that helped.

I searched for ways to call Clojure functions from JS, and it pointed me to this answer on SO. From there, I went to this documentation page. I used that information to export my Clojure functions, compiled a Clojure library, and called those functions from html using something like

<button onclick="console.log(hello_world.core.average(3.4,7.8))">Click me</button>

I think part of the problem is that I don't know JS, so none of this was obvious to me, and evidently the standard use case for JS is something other than creating functions to be called from html when you're clicking buttons. It can be frustrating when you see others writing about how good ClojureScript is, but you can't even figure out how to do basic things.

Top comments (0)