Welcome! Please see the About page for a little more info on how this works.

0 votes
in ClojureScript by

The str function should handle primitive JavaScript symbols produced by Symbol.for. At the moment the str function raises an exception, because it runs into some JavaScript safety checks by using implicit string coersion via (.join #js (link: x) "").
More info on the safety check here: http://www.2ality.com/2014/12/es6-symbols.html
This ticket is also related to:
- http://dev.clojure.org/jira/browse/CLJS-1628
- http://dev.clojure.org/jira/browse/CLJS-890

`
(def x (.for js/Symbol "x"))
(str x)
TypeError: Cannot convert a Symbol value to a string

at Array.join (native)
at Function.cljs.core.str.cljs$core$IFn$_invoke$arity$1 (/home/roman/workspace/clojurescript/.cljs_node_repl/cljs/core.js:9561:12)
at Object.cljs$core$str [as str] (/home/roman/workspace/clojurescript/.cljs_node_repl/cljs/core.js:9543:22)
at repl:1:100
at repl:9:3
at repl:14:4
at Object.exports.runInThisContext (vm.js:54:17)
at Domain.<anonymous> ([stdin]:41:34)
at Domain.run (domain.js:228:14)
at Socket.<anonymous> ([stdin]:40:25)

`

Calling the toString method on a symbol directly works

(.toString x) ;;=> "Symbol(x)"

3 Answers

0 votes
by
_Comment made by: mfikes_

Unit tests fail in (presumably older environments) in Travis:

JavaScriptCore:


ERROR in (test-cljs-1631) (core-advanced-test.js:3724:60)
expected: (= "Symbol(x)" (str (.for js/Symbol "x")))
  actual: #object[ReferenceError ReferenceError: Can't find variable: Symbol]


Nashorn:


ERROR in (test-cljs-1631) (ReferenceError:NaN:NaN)
expected: (= "Symbol(x)" (str (.for js/Symbol "x")))
  actual: #object[ReferenceError ReferenceError: "Symbol" is not defined]


Since this appears to only affect the tests, and not the production code, perhaps the tests could be conditional on {{(exists? js/Symbol)}}.
0 votes
by

Comment made by: thheller

There is a lot of history behind the {{str}} fn/macro and why they do what they to.

See: https://dev.clojure.org/jira/browse/CLJS-890

The patch just uses {{toString}} which is not an option until we sort out CLJS-890.

0 votes
by
Reference: https://clojure.atlassian.net/browse/CLJS-1631 (reported by r0man)
...