This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-06-23
Channels
- # beginners (93)
- # boot (3)
- # cljs-dev (5)
- # clojure (49)
- # clojure-india (8)
- # clojure-russia (1)
- # clojure-spec (4)
- # clojure-uk (37)
- # clojurescript (33)
- # cursive (4)
- # datomic (4)
- # fulcro (11)
- # graphql (1)
- # lumo (16)
- # mount (1)
- # off-topic (21)
- # onyx (2)
- # portkey (4)
- # shadow-cljs (3)
- # spacemacs (4)
- # sql (1)
- # vim (1)
Javascript question (but related to CLJS): This works:
const URL=require("url").URL; new URL("")
but this doesn't: new require("url").URL("")
is there no way to instantiate a class like that without a temporary assignment (in JS or CLJS)?
@pesterhazy Have tried adding parens? New might bind before dot. (Sorry on Mobile)
@lee.justin.m doesn’t work unfortunately
@pesterhazy new (require("url").URL)("
seems to work in node
@lee.justin.m, d'oh, that works
so is that possible in ClojureScript?
cljs.user=> (print (-> (fn [] (new (.-URL (js/require "url")) "")) .toString))
function (){
return (new require("url").URL(""));
}
the generated code sets the parentheses in the wrong place
it should yield new (require("url").URL)(arg)
, not (new require("url").URL(arg))
that’s above my pay grade. i don’t know what guarantees js/require
provides in cljs
In node, (js/require)
is just a function, it could be any other fn
right. so i’m now thinking that the code generation for new
is maybe not ideal:
(print (-> (fn [] (new (.-a (.-b (js-obj))) "arg")) .toString))
=> function (){
return (new {}.b.a("arg"));
}
That’s not right, is it? Don’t think it actually has anything to do with js/require
Right, nothing to do with require
Simple repro:
(let [f (fn [] js/Date)] (new (f)))
... should generate new (f.call(null))()
, not (new f.call(null)())
my misunderstanding?
In JS, new
takes any value
basically what you're saying is that CLJS's new is what works with both Java and Js?
Clojure's new is just Java's new, right?
Wouldn't hurt to generate an extra set of parentheses around the first arg of (new)
though, right?
Not a big deal, the work-around is simply (let [C (f)] (new C arg))
looks like there was validation in place at some point: https://github.com/clojure/clojurescript/commit/dae155a34cd30dabba94f90e306afac930345dcf