This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-04-28
Channels
- # beginners (35)
- # boot (5)
- # cider (16)
- # cljs-dev (22)
- # cljsrn (11)
- # clojars (1)
- # clojure (205)
- # clojure-uk (19)
- # clojurescript (76)
- # cursive (22)
- # duct (4)
- # editors (1)
- # emacs (4)
- # fulcro (1)
- # hoplon (3)
- # leiningen (5)
- # off-topic (25)
- # onyx (29)
- # other-languages (5)
- # parinfer (1)
- # re-frame (13)
- # reagent (8)
- # reitit (2)
- # shadow-cljs (3)
- # tools-deps (11)
- # vim (6)
@mfikes @thheller hmmm, so it's actually a js file getting compiled into go, so i'm not sure if i can use that approach, but i'll try it out
@al.vyguzov There is a #cursive channel - you could try asking in there.
i write a code which cause repl stuck in re-natal i am out get out it nomally :cljs/quit works in react-native environment it not working how can get rid of REPL[======] without restarting again
what’s a common pattern for reading in api keys or other secrets / environment vars to keep them out of source control? in clojure i’ve been using environ.
@chadhs One idea is to built it atop :closure-defines
https://clojurescript.org/reference/compiler-options#closure-defines
If you are using lein
for example, you could set up :closure-defines
as a compiler option, but have the values come from your environment variables by using code like ~(System/getenv "MYVAR")
:closure-defines
is actually easier to use than the docs would lead you to believe https://twitter.com/mfikes/status/979048333850406913
another option is to pass "config" data like this to your app on startup. something like <script src="/js/app.js"></script><script>your.app.init({key:"foo"});</script>
@mfikes that’s pretty interesting, would there be an option to do an either or like read from a file and from system env as a fallback?
ah interesting, should be able to put something together to emulate the clj environ setup then
at a high level it’s handy to have an edn file with “secrets” in a map that are outside of source control, but on deploy have environment vars defined via your deployment pipeline.
are field accessors like -body
considered first-class functions? I’m trying to pass one around as an argument but I’m getting a warning that my-ns/-body
is an undeclared Var. but if i wrap it in a function, the warning goes away
There is memfn
that could be used for functions... but nothing I can think of for field access.
I was just going to say that @mfikes
it can't be a function because property access is not a function call in js afaik
so a wrapper fn is needed if you want to pass it around
okay yes thanks that clarifies things for me. i got confused reading the last example in this block of examples, which passes -body
. i wasn’t thinking clearly and assumed that was a property accessor, which of course it isn’t. does anybody know offhand what it’s referring to? https://github.com/JulianBirch/cljs-ajax#getpost-examples
oh this must be some kind of protocol thing that I haven’t grokked. https://github.com/JulianBirch/cljs-ajax/blob/4edfb1cc0d3a8a7aba29886f63d1d7367ec9d4d6/src/ajax/protocols.cljc#L23
I think this is just a convention used to signify "this is a protocol method"
it doesn't have any actual meaning to the compiler
Stu had mentioned that the -name
convention came about around the time ClojureScript was being built, so it became prevalently used within its standard lib
The reason, IIRC, was to simply avoid collisions (and perhaps to make it look "private")
hm. but i will need to refer the symbol from the library so I’m referring to the right function, right? otherwise it’ll resolve to my-ns/-body
and then it won’t dispatch correctly? sorry for the rudimentary questions--i still have issues figuring out protocols
In other words, it is not a JavaScript method attached to an object that is just "there", like .foo
interesting so you invoke the symbol defined on the protocol, not some implementation
Another example that may help illustrate: #'cljs.core/-count
is the var that -count
resolves to
Maybe to say it another way, the symbols you see in protocol definitions are vars, just like those created using def
or defn
. There may be subtle differences, but the stuff above is accurate.
it's sometimes helpful to see what js is generated
some discussion: http://blog.klipse.tech/clojurescript/2016/04/09/clojurescript-protocols-secret.html
The really cool thing about this is that protocols don't push you into a different model of working than working with functions created via defn
. They, of course, have interesting dispatch semantics, based on the first argument, but otherwise they dovetail right in with the way the rest of the language works with functions.
If you (set! *print-fn-bodies* true)
and evaluate -count
you will see a little of how it works
very helpful (to me) explanation @mfikes
If you are wondering how -count
works, on, say [1]
try (js-keys [1])
and you will see this in there: "cljs$core$ICounted$_count$arity$1"
okay I think I see. so you invoke the functions in the protocol, which do dynamic dispatch on their first arg. the various implementations for a given type get “attached” as they are loaded, somewhat similar to how the various implementations of a multimethod are loaded as you go. deftype, defrecord, and reify are just ways to providing an implementation for a given type. is that about right?
Yeah. The mechanics can change too. In Clojure they are kept in a separate place, so that you can extend a protocol to something that you can't modify, like String
i think the part of this that eluded me is what impact it has to define a protocol with multiple methods. because in some sense each method seems to operate independently
Yeah, I think when satisfying a protocol you are supposed to satisfy all of the methods. But it seems you can get away with implementing a subset, and things still work.
I'd have to go rummage through some Clojure references to clear up that last bit in my mind.
FWIW, I initially thought that you could, say refer to -body
directly, with this mental model I think coming from my Java experience. I suspect it is a very common misunderstanding made initially by new Clojure devs.
now that i think about it, it’s the multiple-methods-in-a-protocol bit that has confused me. it has always seemed like it’s more about good self-documenting code than a piece of technology, but i’ve just been assuming that i’ve missed some critical bit
Another interesting aspect is that protocols are meant to be easy for the implementors to satisfy; they aren't really there for clients. With that comes the recommendation that protocols have very few methods.
Hi, How would one instantiate a nested javascript object? LIke Vimeo.player I've tried
js/Vimeo.Player
(.-Player js/Vimeo)
(js/Video.Player.)
should work as well
or (new (.-Player js/Video))
In case this looks a bit mysterious: A dot at the end is handled during macroexpansion.
(macroexpand '(js/Video.Player.))
will illustrate a 4th way to construct a PlayerFor some reason, I still can't bring myself to put dots in symbols.
(let [x #js {:foo #js {:bar 42}}]
x.foo.bar)
just bothers me