3

I'd like to implement a Foxx service using ClojureScript.

I've read that one can use Typescript and Coffeescript by running the transpiler during each development step;

Can I do similar for ClojureScript?

dothebart
  • 5,972
  • 16
  • 40

2 Answers2

2

I had great time using Clojurescript with Foxx and the result is this open source library: https://github.com/arango-cljs/forest The reason I stopped working on this two years ago is that the REPL workflow wasn't good enough and the lack of Websocket. But Clojurescript-in-Clojurescript will be available soon, the Boot build tool was born and I guess ArangoDB+Foxx have been much improved since then. If you are interested, we can discuss.

myguidingstar
  • 673
  • 4
  • 10
1

As far as we know, it is not possible to write ClojureScript applications in such a way that they could run in ArangoDB/Foxx.

Unlike TypeScript and CoffeeScript, ClojureScript is not just a language but an application runtime. It's better to think of it not as an alternative syntax for JavaScript but as a way to write applications that happen to be executed on a JavaScript engine. In other words, although it's possible to write re-usable ClojureScript modules, it's designed for writing standalone ClojureScript applications, not arbitrary JavaScript modules.

Because it was originally designed to be run in the browser and thus not written with Node-like module systems in mind, it uses a global namespace via the Google Closure Compiler. This means running multiple applications in the same context would result in namespace conflicts.

Also, Foxx services are expected to be stateless. Any state must be persisted in collections or the server response because Foxx services are executed in different V8 contexts each time. ClojureScript on the other hand is stateful by definition because as a Lisp it considers code just a special form of data.

As a rule of thumb: languages designed to be transparent substitutes for the JavaScript language (like TypeScript, CoffeeScript, LiveScript, PureScript) should work without any problems. Languages designed to write standalone applications (like ClojureScript and Elm) most likely won't work.

In any case, if you want to use an alternative language (or Babel) you will have to transpile the code outside of ArangoDB and only include the generated JavaScript output in your Foxx bundle. In ArangoDB 3.0 you will be able to use a single entry point with a require hook as in Node.js but we still recommend precompiling your code for performance reasons and to make it easier to catch compile time errors.

Alan Plum
  • 10,814
  • 4
  • 40
  • 57
  • Clojurescript is NOT an application run time. It's just like Typescript and Coffeescript but with a not-so-small core library. It also needs a runtime like browsers, nodejs and nashorn. – myguidingstar May 12 '16 at 15:45
  • Google Closure Compile will rename var names to shorter ones, but that won't cause namespace conflict problems. Also the problem of Nodejs style vs Google Closure style modules have been resolved years ago – myguidingstar May 12 '16 at 15:53
  • The point "ClojureScript on the other hand is stateful by definition because as a Lisp it considers code just a special form of data." doesn't make any sense. Clojurescript just works! Stateful vs stateless in Lisp family is the matter of coding style, not language level. – myguidingstar May 12 '16 at 15:54
  • @myguidingstar: I'm just going by the explanation the clojurescript community on FreeNode gave me (basically: clojurescript needs to be able to evaluate code at run time, thus the comment about the "application runtime") and my own failed attempts to use cljs in Foxx. The output used the global "goog" namespace from the Google Closure Compiler. If you could elaborate on the steps you took to develop Foxx services with ClojureScript that would be an awesome addition. – Alan Plum May 17 '16 at 22:02