This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-05-13
Channels
- # beginners (17)
- # boot (16)
- # cider (13)
- # cljs-dev (26)
- # cljsrn (5)
- # clojure (155)
- # clojure-belgium (2)
- # clojure-spec (19)
- # clojure-uk (4)
- # clojurescript (32)
- # community-development (16)
- # core-async (12)
- # cursive (3)
- # datomic (7)
- # hoplon (14)
- # lambdaisland (1)
- # lumo (16)
- # microservices (1)
- # off-topic (3)
- # om (5)
- # onyx (4)
- # protorepl (1)
- # re-frame (1)
- # rum (1)
- # specter (6)
- # unrepl (32)
Is there a technique for a cljc to import a class only when the target lang is Clojure? Closest I can get is an import when cljc is run as clj, which means an unnecessary import when target is cljs (i.e. During macro-time cljs)
@favila what about doing it in a macro that knows which context it’s being expanded in?
(when (nil? (:ns &env)
(import ...))
oh hrm, it might be
happy to look at it
ok, compiling this:
(ns clj-only-import.core)
(defmacro when-clj-target [body]
#?(:clj
(when-not (:ns &env)
body)))
(when-clj-target
(import '(java.util UUID)))
I get this: Caused by: clojure.lang.ExceptionInfo: No such namespace: java.util.UUID, could not locate java/util/UUID.cljs, java/util/UUID.cljc, or Closure namespace "java.util.UUID" at line 9 src/clj-only-import/core.cljc {:file "src/clj-only-import/core.cljc", :line 9, :column 3, :tag :cljs/analysis-error}
// Compiled by ClojureScript 1.9.473 {}
goog.provide('clj_only_import.core');
goog.require('cljs.core');
clj_only_import.core.when_clj_target = (function clj_only_import$core$when_clj_target(_AMPERSAND_form,_AMPERSAND_env,body){
return null;
});
clj_only_import.core.when_clj_target.cljs$lang$macro = true;
(ns clj-only-import.core)
(defmacro when-clj-target [body]
#?(:clj
(when-not (:ns &env)
body)))
#?(:clj (when-clj-target
(import '(java.util UUID))))
(when-clj-target
(def my-uuid (UUID/fromString "8668a394-1caf-4e1f-a622-fd8fa5929b3f")))
will warn:
WARNING: No such namespace: UUID, could not locate UUID.cljs, UUID.cljc, or Closure namespace "" at line 12 src/clj_only_import/core.cljc
WARNING: Use of undeclared Var UUID/fromString at line 12 src/clj_only_import/core.cljc
@dnolen any idea why the analyzer would analyze cljs.core twice? currently fighting some :redef-in-file
warnings when I change the behaviour of declare
. https://dev.clojure.org/jira/browse/CLJS-1992?focusedCommentId=45595&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-45595