Fork me on GitHub
#clojurescript
<
2019-03-03
>
Vishal Gautam00:03:57

Hi I am trying to use spec library in my re-frame project. When I try to run the code in repl it doesn work and I get this error CompilerException java.lang.ClassNotFoundException: cljs.spec.alpha, compiling:. and CompilerException java.lang.RuntimeException: No such var: s/valid?

Vishal Gautam00:03:04

Here is the source:

Vishal Gautam00:03:31

(ns app.db
  (:require [cljs.spec.alpha :as s :include-macros true]))

;; Modeling Application state
(s/def ::name string?)
(s/valid? ::name "John Doe")

podviaznikov03:03:49

hey everyone. I have a bit trouble with with class.eval function. I have following wrapper for evaluating strings in clojurescript in nodejs

(defn eval-str [s]
      (eval
        (empty-state)
        (read-string s)
        {:eval       js-eval
        ;  :load (fn [_ cb] (cb {:lang :clj :source "(ns foo.core) (defn add [a b] (+ a b))"}))
         :source-map false
         :verbose    true
         :context    :expr
        ;  :ns 'foo.core
         :load-macros true}
        (fn [result] 
          (println "result>>" result)
          result
        )))
It works in simple cases: (eval-str "(do (defn add [a b] (+ a b))(add 4 6))") However I'm making trouble to make this work if I want to use library in the code. E.g. I want to use curds library:
(eval-str "(do (require '[cuerdas.core :as cue])(cue/slug \"Hello\"))")
I see that class eval has different options. Like load-fn, include-macros, even include dependencies. I tried different variations but failing for now. Maybe someone knows about working code examples on blogs in similar topic?

mfikes05:03:07

@podviaznikov require is a top-level thing in ClojureScript (it can’t be properly processed inside a do)

mfikes05:03:36

There is a cljs.js/eval-str function that might be more straightforward to use

mfikes05:03:08

The biggest challenge will be in defining the load-fn; I'd recommend perhaps taking a look at the Lumo codebase for an example of how this can be done with Node

Yehonathan Sharvit06:03:22

Is it safe nowadays to access a nested javacript property like this (.. e -target -value)? Will it be properly handled by advanced compilation?

valtteri09:03:21

@viebel there’s a compiler option :infer-externs that works in many cases. However it’s not bullet proof but you can add (set! *warn-on-infer* true) on top of the file you’re working with to see if it’s working or if you need to provide extra hints for the compiler to generate needed externs.

valtteri09:03:16

If you want to play safe you can use libs from cljsjs where someone else has already created the externs file for you.

Yehonathan Sharvit11:03:57

What about code that relies on native javascript properties like (.. e -target -value) in a event handler?

Yehonathan Sharvit11:03:44

Yeah. Currently, I am using purnam but it is not maintained any more. So I’ll probably migrate to https://github.com/appliedsciencestudio/js-interop/ @rakyi

valtteri13:03:47

AFAIK there’s no problem with any of the cljs oob interop patterns with :advanced unless you’re accessing objects/functions in :foreign-libs.

valtteri13:03:42

So I’d say that using a lib to access object properties is unnecessary, unless you’re working with :foreign-libs.

mhuebert08:03:19

Browser events & npm interop (eg with shadow-cljs) also require some kind of special attention to property access / method calls