I'm just trying out ClojureScript, starting out by converting something I wrote in Clojure into cljx.
When I try to compile it I get:
clojure.lang.ExceptionInfo: :refer must be followed by a sequence of symbols in :require
I'm finding some oblique references online, but nowhere where it's spelled out whether I should be able to use a :refer :all in a ClojureScript program.
If I can't do it, what's the reason for this restriction?
No, it's intentionally not possible. There was a conversation on the ClojureScript mailing list recently related to :refer :all and it looks like it will never be supported.
To quote David Nolen from that thread:
It's just bad style and as far I know the only reason it hasn't
changed in Clojure is because the core team is very adamant about
preserving backwards compatibility when possible. The conspicuous lack
of naked :use in ClojureScript was intentional.
Related
I'm new to Clojure but my end goal in learning the language is to use the Clojurescript compiler, since I plan on using the generated code on the browser VM.
Since there tutorials are abundant for Clojure and Clojurescript is just another compiler I'm doing the Clojure tutorials, but now I got to a point where I want to know where is the line between the libraries that are core clojure that will compile to javascript fine and the ones that won't.
For example, in the tutorial I'm following for Clojure I was reading this but it seems this is already using deep Java objects, and not core Clojure commons:
user=> (.length a-string)
7
user=> (.substring a-string 3)
"name"
user=> (.substring a-string 3 5)
"na"
// And for static methods
user=> (def rt (.getRuntime Runtime))
#'user/rt
user=> (.freeMemory rt)
30462304
The beginning of the chapter said In Clojure, strings are the same as Java strings, but not that Clojure uses String from Java, which is a different thing.
So, if my goal is to develop Clojure targeting the browser, what APIs and types can I use? Is there a simple distinction for this? I imagined that anything that doesn't need to be explicitly required (core libs) would be core Clojure and I wouldn't be tying myself the Java libs.
Thank you in advance.
If you want to write code to target both Clojure on the JVM and ClojureScript, see cljx.
Without using such tools (to embed code snippets explicitly written for either backend), it is not generally expected that substantial programs will work against both independently.
See: https://github.com/clojure/clojurescript/wiki/Differences-from-Clojure#namespaces
It bothers me that idiomatic clojure is:
(ns clojure-example
(:require [clojure.set :refer [union]]))
But idiomatic clojurescript is:
(ns clojurescript-example
(:use [clojure.set :only [union]]))
Of course, the clojurescript code would work in clojure as well, but that brings out demons who say I should use require with :refer instead!
What is the cause of this?
Actually ClojureScript does support :require :refer and has supported it for a long while (here's my commit introducing support for :refer from 12 Jun 2012). That wiki page is out of date. I have edited the section on namespaces on the wiki to bring it up to date.
As for what is idiomatic, there are of course people who dislike :use, but that hardly makes it unidiomatic after several years of production use. You're free to make up your own mind as to whether you prefer :require :refer or not.
(Although the use case where :use actually offers capabilities that :require does not -- pulling in several namespaces with (:use lib.foo lib.bar lib.quux) -- is indeed not supported in ClojureScript, by design.)
are there any documentation on clojure built-in java method?
for example, .toUpperCase from java.lang.String and .indexOf from clojure.lang.PersistantVector.
Is there anyway I can find useful java method without looking at the source code?
As others have pointed out, you can get the java.* and javax.* documentation online pretty easily as it is part of core Java.
For the clojure.*, your best reference is the source. Even so, I'd recommend not relying on it since this code should really be considered an implementation detail of Clojure. You have no guarantee that the functionality won't change in future versions of Clojure and break any code that depends on it.
How about the Java API? All of Java's classes and methods are listed there. That covers all of the "clojure built-in java methods".
On the other hand, Clojure's classes are documented in here, Clojure's API. You have to learn to distinguish between Clojure's classes and Java's classes. All packages starting with java.* or javax.* belong to Java and are documented in the first link. The packages starting with clojure.* are from Clojure, you'll find their documentation in the second link.
If the package for the class starts with java or javax then you can look it up in the Java documentation on Oracle's website.
For Clojure implementation classes (where the package name starts with clojure) you are probably stuck with looking at the source code. There is documentation for the intended API (the Clojure language) but not for the Java classes implementing it. You may be able to find some articles (like this one) depending on if what you're looking for is something a blogger has taken an interest in.
My question is NOT about how to use ClojureScript to produce JavaScript code.
I am interested in ClojureScript because it implements Clojure \ {eval} within Clojure, and is able to compile it to another language. Thus, I'm interested in the possibility of having ClojureScript target other platforms.
Question: besides the source code, is the Design & Implementation of ClojureScript documented anywhere? I'd like a high level overview of how the various parts of the compiler work together:
* how are the
As far as the documentation for "Clojurescript pipeline and how you can hook into it" is concerned you can check out this blog entry.
I have started building a system with clojure, mainly because I need to use Java libraries. My main problem with Clojure is lack of proper IDE support (getting it to work well with Emacs on Windows was not trivial). I was wondering what difficulties other people have had.
Lack of "user friendly" stacktraces (coming from Haskell, it felt like a giant step back), but you get used to it eventually and learn to work your way from slime/swank.
Still having nightmare about the days when we didn't have leiningen (classpath mess, start scripts, dependency "management" hell).
It improved a lot and is improving every release it seems.
getting bitten by the "lazy bug".
(with-open [file (writer name)]
(map #(.write file (process %)) (get-data)))
and "the lazy bug" makes your file empty!
ps: the answer is dorun
An idea: if you are working in a Java environment then you might consider sticking with your Java IDE and use a Clojure plugin rather than going with Emacs etc.
For example, my setup works beautifully with:
Eclipse 3.6.1
Counterclockwise plugin for Clojure 0.2.0 RC1 (http://code.google.com/p/counterclockwise/)
Clojure 1.2 libraries (either on the eclipse build path, or automatically imported using Maven)
Interactive development using the REPL provided with Counterclockwise (nREPL)
Since I need to use a lot of Java along with my Clojure code (often in the same project!), this setup makes much more sense than wrestling with a whole new set of tools.
My problems so far:
It wasn't too easy to get EMACS/SLIME with Common Lisp AND Clojure.
Clojure 1.2.0 stacktraces are a mess so far. It's often so hard to get it what went wrong.
The debugging experience is not very nice. Tried JSWAT and Counterclockwise, but not really happy with it.
Changing my mindset from imperative to functional programming.
It got better after I read a book on lisp programming.