Fork me on GitHub
#clojurescript
<
2015-12-28
>
mudphone00:12:22

Hi, I’m trying to require cljs.analyzer.api to use resolve but am getting errors, is this not possible?

crocket00:12:04

@mudphone: Are you going to require it in clojurescript?

crocket00:12:12

cljs.analyzer.api is a clojure namespace. You have to use it in clojure.

mudphone00:12:14

@crocket: ah, I see, okay

mudphone00:12:22

I wanted to use it in cljs

crocket00:12:38

compiler API belongs to clojure.

crocket00:12:10

If you really want to use compiler API in clojurescript, you have to compile it to clojurescript, first.

crocket00:12:24

Compile clojurescript to clojurescript.

crocket00:12:46

Bootstrapped clojurescript is not a full substitute for clojurescript compiler on JVM.

mudphone00:12:24

@crocket: got it, actually was just hoping to turn a key or string into a function and call it at runtime

crocket02:12:00

@mudphone: Evaluating a string into a function creates security vulnerabilities in practice.

crocket02:12:29

It allows crackers to execute arbitrary code in remote programs.

crocket02:12:03

Crackers will find security vulnerabilities, given enough time.

cjmurphy03:12:11

When doing lein figwheel it seems I get

Caused by: java.io.FileNotFoundException: Could not locate cljs/analyzer__init.c
lass or cljs/analyzer.clj on classpath:

cjmurphy03:12:46

I think it is to do with there being devcards defined.

dnolen03:12:18

@cjmurphy: this nearly always means you haven’t specified a Clojure 1.7.0 dependency explicitly

cjmurphy03:12:05

Okay thanks, was just going straight off some other project.clj. Will check...

cjmurphy03:12:52

[org.clojure/clojure "1.7.0"]
                 [org.clojure/clojurescript "1.7.170"]

cjmurphy04:12:04

I just cloned devcards and then did lein figwheel.

cjmurphy04:12:46

git clone 

cd devcards

lein figwheel

cjmurphy04:12:38

I had the same issue with the devcards template, so put up this issue:

dnolen04:12:26

@cjmurphy: but you also need to be sure nothing is pulling in the wrong dependency, lein deps :tree

cjmurphy04:12:31

I'll do it manually. Wanting to get kanban demo going - seems the most comprehensive Om/Next demo out there...

cjmurphy04:12:43

Okay will do thanks..

cjmurphy04:12:00

I'm on Windows so boot does not work.

dnolen04:12:06

@cjmurphy: sure, but in general I recommend taking the slower path

cjmurphy04:12:09

Have to convert it to lein.

dnolen04:12:31

do the tutorials - then figure out why the template isn’t working based on that

cjmurphy04:12:24

I've done all the tutorials. Jannis's code looks easy enough to follow - good to see lots of querys at the root level.

dnolen04:12:17

@cjmurphy: ok cool, just double checking

dnolen04:12:25

lots of people just jump into the deep end

mudphone04:12:41

@crocket: understood… actually, I’m not taking the string from the user, just want to use a keyword, turn into a string, and resolve into an already existing function

crocket06:12:55

Is there a good library for turning a callback hell into something better?

rm06:12:45

core.async :)

crocket06:12:08

core.async doesn't handle errors by default. Promise does, though.

rm07:12:57

crocket: in go they use values as errors. Also you can define your own error protocol. Maybe it could be helpful

crocket07:12:23

values as errors? Can you show me an example?

rm07:12:12

https://github.com/jamesmacaulay/cljs-promises or there is some promises library over core.async @crocket

crocket07:12:44

Since errors are emitted from the same channels as other values, it is inconvenient. Exceptions can't be used, either.

rm07:12:12

hmm... what about Failure record? I wrote a blog-post about it, but I'm not very experienced with core.async: https://s-mage.github.io/2015/10/16/better-errors.html

crocket07:12:07

Typed Clojure may be a better alternative. Errors are of different types from other values. Something like Either<Error,HttpResponse> is not bad.

rm07:12:05

yes, it is

rm07:12:22

I guess cats is the most advanced library for it now

jindrichm09:12:55

Using the regexp from http://stackoverflow.com/a/8317014/385505 in Clojurescript throws "Invalid regular expression flags". Why is that? EDIT: I see, forward slashes don't need to be escaped in Clojurescript.

jimmy10:12:57

hi guys, i have this simple function to catch keyboard shortcut, it doesn't seem to work, does anyone spot something wrong ?

(ns something
   (:require [goog.events :as events])
   (:import [goog.ui KeyboardShortcutHandler]))
;; ...
   (let [handler (doto
                     (KeyboardShortcutHandler. js/document)
                   (.registerShortcut 'A' 'a'))
         triggered-fn (fn [e]
                        (js/alert (str "Shortcut triggered" (.identifier e))))]
     (events/listen handler
                    js/goog.ui.KeyboardShortcutHandler.EventType.SHORTCUT_TRIGGERED
                    triggered-fn)
     )

jaen10:12:28

@mudphone: why not keep the functions in a map and resolve it that way? Something like:

(let [functions {:keyword (fn [arg] (println "wheee" arg))}
      function  (functions :keyword)]
  (function "test'))

jaen10:12:37

@crocket: as someone mentioned using Maybe and Either from cats - http://funcool.github.io/cats/latest/#maybe - could be a good solution.

rhansen13:12:48

@crocket: Use the go-catching and <? macros from the glossop libraries. They are slight variations of the go and <! macros from core.async that catches exceptions.

rhansen13:12:01

*they build upon the macros from core.async

phil14:12:32

How do people go about testing cljc at the moment?

phil14:12:56

Is there an 'easy' way of testing cljs and clj in the same project?

niwinz14:12:46

@phil: we are already doing it on our projects at funcool github organization

phil14:12:49

How do I run the cljs tests?

niwinz14:12:57

node out/tests.js

phil14:12:33

Awesome - I'll base it on that.

niwinz14:12:25

If you have small number of tests octet approach is much simplier, but if you are expecting have a bunch of namespaces, cats approach is more appropriate

phil14:12:16

I'll look at octet too - thanks again.

niwinz14:12:48

you are welcome!

vibl15:12:00

I can't make macroexpand nor macroexpand-1 work in a macro called from CLJS. Does anyone have this problem?

vibl15:12:25

(defmacro test-macroexpand [] `(quote ~(macroexpand-1 '(-> 1 inc)))) returns (-> 1 inc) instead of (inc 1)

magomimmo15:12:58

@vibl: sorry, I did not read quite well you question...

darwin15:12:26

how to test that function didn’t throw in cljs.test? opposite of (is (thrown? …))

vibl15:12:54

Is defmacro supposed to work on a cljs repl?

magomimmo15:12:06

@vibl: no. You can only macroexpand it. defmacro is still clojure, not clojurescript

vibl15:12:21

A macro declared from the cljs repl with defmacro seems to work (which surprised me) but with an error message: "WARNING: Wrong number of args (0) passed to cljs.user/bla at line 1 <cljs repl>"

vibl15:12:58

@magomimmo try it, you'll be surprised.

magomimmo15:12:19

@vibl: surprised as well

magomimmo15:12:20

@vibl: someone from the dev team will give us an answer

vibl15:12:37

@magomimmo: maybe it's a feature preview simple_smile

dnolen15:12:56

@vibl: just an unintended side effect from bootstrapped

magomimmo15:12:02

@vibl: people are already abusing defmacro….imagine what could happen if they make it available in cljs as well simple_smile

dnolen15:12:18

as stated in a few places already defmacro will never work in .cljs source files

vibl15:12:36

@dnolen That's why I was surprised that defmacro works in my figwheel repl

dnolen15:12:21

it’s a minor issue that it doesn’t just throw an error, someone can submit a patch for that if they like

vibl15:12:26

@dnolen On an unrelated subject: I can't make macroexpand and macroexpand-1 work within a clj macro called from cljs.

dnolen15:12:59

@vibl can’t help you with that

dnolen15:12:04

neither of those things are well supported

dnolen15:12:29

you can examine the analyzer if you want to sort out how to make it work

vibl15:12:09

@dnolen: clj macros are not well supported in cljs?

dnolen15:12:23

@vibl now I don’t understand your question at all simple_smile

dnolen15:12:38

in anycase in general there just no need to direct questions at me

dnolen15:12:46

there’s a #C07UQ678E channel with knowledgeable people

vibl15:12:52

@dnolen: in a clj file, there is (defmacro test-macroexpand [] (quote ~(macroexpand-1 '(-> 1 inc))))`

dnolen15:12:06

I’m trying to hint I don’t have time to answer your question

dnolen15:12:10

ask in #C07UQ678E

vibl15:12:21

@dnolen: ok, understand simple_smile

mheld18:12:18

is there any documentation or anything about preferred document organization in a SPA?

mheld18:12:48

models/controllers/views etc

niwinz18:12:57

I think that there is no single solution for that...

niwinz18:12:27

the best documentation maybe is the source code organization of the existing open source applications

roberto18:12:57

that comes down to personal taste.

mheld18:12:09

hah a'ight

mheld18:12:17

imma use my own opinions simple_smile

mudphone20:12:14

@richiardiandrea: Thanks, that gets me closer, I’m still seeing some odd errors, mostly because I probably have to dig into this source. Here’s my first attempt in case you’re interested:

mudphone20:12:22

(ana/resolve-var (empty-analyzer-env) (symbol "+") ana/confirm-var-exist-warning)
#object[Error Error: No protocol method IDeref.-deref defined for type null: ]

mudphone20:12:44

@jaen: Thanks that would work. I’m using a multimethod now, I think someone here recommended. I was really just trying to see if I could get clever and only have to specify the fn names twice, instead of three times.

jaen20:12:23

Multimethod seems legit as well, I suppose.

jaen20:12:40

But basically you can't select a function by string at runtime in Clojurescript, so you have to get creative.

mudphone21:12:52

@jaen, I guess I could pull all the fns from an ns and put them into a map, where I turn the fn names into keys?

jaen21:12:18

Well, but not at the runtime.

jaen21:12:36

You could do either write it out manually or write a macro using the analyser I suppose.

heeton22:12:23

http://pastie.org/10657931 How could I resolve this keyword in cljs? It looks like clojure’s #resolve would work, but that’s not available

jaen22:12:15

@heeton: make a map from keywords to functions.

spelufo22:12:36

HI, should (extend-protocol IDoThing number (do-thing [this] ...)) work in clojurescript?

spelufo22:12:09

I get the error: #object[TypeError TypeError: Cannot read property 'do_thing' of undefined]

spelufo22:12:37

The problem only arises with numbers.

heeton22:12:12

@jaen: I’ll have hundreds of these, is there no automated way?

jaen22:12:29

write a macro, I suppose.

jaen22:12:54

You can use resolve in Clojurescript at macro expansion time.

jaen22:12:59

Just not at runtime.

christopherbui22:12:44

@heeton: What are you trying to do exactly?

christopherbui22:12:36

@spelufo: Don’t you want capital N number?

spelufo22:12:21

no, that throws another error, and I've seen lowercase number used in the clojurescript and garden's source code.

heeton22:12:25

@christopherbui: trying to create a router. I have a bunch of pages defined and want to be able to transition, the result of my function being a reference, not the value

spelufo22:12:43

And stepping through the extend-protocol form in the chrome devtools it skips that type when going through the list.

spelufo22:12:22

I'm using figwheel with cider, don't know if that has anything to do with it.

niwinz22:12:02

@jaen I'm not sure about resolve, one month ago I have tried to something that requires resolve and I found that cljs does not provide it :S

niwinz22:12:56

I have asked some questions in the mailing list... but I don't found any solution at this moment

cjmurphy22:12:02

I'm having trouble with goog.string.format being recognised as a function. Could there be something obvious I'm doing wrong?

jaen22:12:48

IIRC it should be available during macroexpansions in Clojurescript

christopherbui22:12:20

@spelufo: Are you sure? I tried this out in the cljs repl and it works

cljs.user=> (defprotocol IDoThing (do-thing [this]))
nil
cljs.user=> (extend-protocol IDoThing js/Number (do-thing [this] 1))
#object[Function "function (this$){
var this$__$1 = this;
return 1;
}"]
cljs.user=> (do-thing 23)
1
cljs.user=> 

niwinz22:12:12

I'll try to revisit it again, now knowing about resolve.

spelufo22:12:06

Yes I just tried it and it works in the repl, but not in my project.

spelufo22:12:42

Very weird. It's the same code. Maybe figwheel is responsible?

jaen22:12:30

@niwinz: hopefully it will be helpful; I didn't have need to try that out yet, but from what I understand it should work.

cjmurphy22:12:39

I don't think format is a macro. I just require it like: [goog.string :refer [format]], and call it the way you might call any function. But Cursive and then running figwheel/devcards - so both at code writing and code running times - do not think it is a function. I don't believe there is anything special I need to do in the project.clj file.

niwinz22:12:02

@jaen: thanks for pointing me on it 😉

fappy22:12:41

hello simple_smile If you have a minute, I've posted a question about following the QuickStart guide in getting a node REPL in the google group: https://groups.google.com/forum/#!topic/clojure/pf2GuFVtiAw Basically when I try to build with the prescribed node.clj, it can't find a dependency

fappy22:12:54

but lein cljsbuild once works

spelufo23:12:41

I've stopped on the exeption in the devtoold and the line that is making trouble is my-ns.core.my-ns.core.do_thing[goog.typeOf(3)]. Don't know how it got to that, but clearly it should be my-ns.core.do_thing[goog.typeOf(3)], the problem is that the namespace gets duplicated somehow.

spelufo23:12:29

I had a (def sth ...) in a namespace '(ns sth.core)', and when I changed it to (def sth-else ...) the duplicate sth.core.sth.core turned into sth.core.

spelufo23:12:53

This is clearly a bug.

cjmurphy23:12:58

Is there a way of giving a namespace you use a different name within the require, for example (which is bad syntax): [goog.string :refer [format :as format-str]]?

cjmurphy23:12:53

I want to use a name other than format in case there is some kind of shadowing problem...