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

0 votes
in ClojureScript by

^:export doesn't appear to have an effect on defrecord - I only see Closure emitting the original symbols for def and defn.

I guess that similarly, deftype, defprotocol etc also would be reasonable targets.

In any case, it would be good to explicitly state what works and what does currently not (advanced-compilation.adoc), so one doesn't find himself blindly trying out things.

6 Answers

0 votes
by

Comment made by: mfikes

You can export protocol methods. Proposed site change: https://github.com/clojure/clojurescript-site/pull/148

0 votes
by
_Comment made by: mfikes_

{{^:export}} works on (and only on) Vars.

Protocol methods are Vars, so the above site change would help clarify how to export them.

The remaining Vars under consideration would be the map and positional factory functions and constructors that are synthetically generated for {{defrecord}} and {{deftype}}.

Perhaps a change could be accepted such that {{(defrecord ^:export Foo [])}} would export {{map->Foo}}, {{\->Foo}}, and {{Foo}}. And likewise {{(deftype ^:export Bar [])}} would export {{->Bar}} and {{Bar}}.
0 votes
by

Comment made by: mfikes

Workaround: Use {{goog/exportSymbol}}:

`
(ns my-ns.core)

(defrecord Foo [])
(goog/exportSymbol "my_ns.core.mapGT_Foo" my_ns.core.mapGT_Foo)
(goog/exportSymbol "my_ns.core.GT_Foo" my_ns.core.GT_Foo)
`

0 votes
by

Comment made by: vemv

Hi Mike,

thanks so much for clarifying and taking care of this one! Appreciated.

??^:export works on (and only on) Vars.??

This would be an excelent piece of knowledge to include in the documentation IMO. Why not tell the whole story in a single place?

The original concern that made me open this issue was logging/debugging.

Say an application has a series of singleton defrecord instances, as part of a 'component' system (Sierra style).

Then, for logging, I might be tempted to {{(js/console.log (type the-instance))}}, so I can identify e.g. a buggy component.

But without language support, one is unable to get that (or something similar) to work without lots of boilerplate, or perhaps a hacky macro.

Would it be possible to make {{(type some-instance-of-a-defrecord-or-deftype)}} print the original name under advanced compilation?

0 votes
by
_Comment made by: mfikes_

Hi Victor,

Thanks for the ticket intent clarification. {{\^:export}} is not for debugging, but is for making un-renamed symbols available to JavaScript. In fact, it doesn't change renamed and optimized JavaScript, but instead simply makes additional un-renamed aliases available which point into optimized / renamed code. (Thus, generally, debugging deals with the renamed / optimized code, even if there are un-renamed {{^:exports}} sitting off to the side.)

Fortunately, {{:pseudo-names}} exists to help with debugging optimized code: https://clojurescript.org/reference/compiler-options#pseudo-names

For your example involving logging the type of the instance, instead of, for example {{[Function: ze]}} (under Node.js), you would get {{[Function: $my_ns$core$Foo$$]}} if {{:pseudo-names}} is enabled.

I've proposed revisions to the site: https://github.com/clojure/clojurescript-site/pull/150

If {{:pseudo-names}} satisfies the needs of this ticket, perhaps this ticket can be closed.
0 votes
by
Reference: https://clojure.atlassian.net/browse/CLJS-2406 (reported by alex+import)
...