This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-03-14
Channels
- # beginners (74)
- # boot (23)
- # braid-chat (7)
- # cider (5)
- # clara (3)
- # cljsjs (17)
- # cljsrn (1)
- # clojure (105)
- # clojure-austin (9)
- # clojure-new-zealand (34)
- # clojure-poland (2)
- # clojure-russia (177)
- # clojure-uk (41)
- # clojure-ukraine (2)
- # clojurescript (130)
- # component (1)
- # core-async (2)
- # core-matrix (6)
- # cursive (7)
- # data-science (103)
- # datomic (24)
- # emacs (15)
- # funcool (4)
- # hoplon (21)
- # immutant (151)
- # ldnclj (76)
- # melbourne (1)
- # off-topic (8)
- # om (152)
- # om-next (1)
- # onyx (26)
- # parinfer (38)
- # re-frame (13)
- # reagent (14)
- # spacemacs (1)
- # vim (92)
- # yada (1)
Wrote up a bit on Records in Clojure on my blog: http://blog.clojuregeek.com/2016/03/13/records-in-clojure/
I have read https://github.com/clojure/clojurescript/wiki/Using-cljc but I still can’t understand why we need cljc
Never mind I found http://clojure.org/guides/reader_conditionals
urbanslug: here's an example of where I use cljc - I have a custom URL generator/parser that saves state we consider "bookmarkable" in the fragment, so that when a client bookmarks the view of the SPA, they get a recreation of view state.
urbanslug: we want to be able to redirect clients from the backend to a specific view state, for which we need to be able to generate a specific encoded URL
at the same time, we also need to be able to decode and encode the URL from the frontend, in order to maintain bookmarkability of state
this is easy to handle with a namespace for URL manipulation and parsing in cljc code. Much better than keeping a clj and a cljs namespace in sync.
np, glad I could help
@noisesmith: What’s an SPA though
sorry, "single page app"
which means that when you click a link in-app, it doesn't actually send a new GET request to the backend, it just does some transforms with the data it already has in browser
gmail is an SPA for example
there are also websocket and ajax requests to get new data from the backend, but those are decomplected from page changes - they happen when you need new data, which may or may not be on an href click event
and all this is why we want to put relevant view state in the fragment of the page - so that parts of the app are still bookmarkable
brb, meeting
@noisesmith: Same thing happens with linking different parts of code or a github readme?
@urbanslug: yeah, similar in the github app
hey all, super basic thing I’m struggling with that I’d love to get some help on. I’m sure there’s a simple answer, I just can’t find it from perusing docs.
trying to figure out how to import a Java library. declared the dep in my project.clj, it looks something like [com.someOrg/some-lib “1.0.0”]
. leiningen pulled the library successfully when running lein deps
. when attempting to use that library in my clojure project, however, I can’t seem to figure out the correct construction to import
the library into my clojure file.
would it be something like:
(ns myproject.core (:import (some-lib)))
or (ns myproject.core (:import [some-lib]))
?
as a followup, what is actually getting imported? let’s say the library itself exports a single public constructor for SomeObject
. How do I instantiate an instance SomeObject
in clojure?
really appreciate any help anyone can provide.
@bwstearns: thanks for the suggestion. I was under the impression that it’s idiomatic to import
Java things and only require
when it’s clj(s) libs
yeah I mean I can’t even seem to grab a reference that my repl recognizes to the publicly exported SomeObject
constructor. which makes me think I’m just importing the library incorrectly
docs are pretty clear on what to do when it’s an already-included java library, not so much when it’s a third party java lib
Is it erroring [edit] when you try to require/import[/edit] or is instantiating SomeObject not working?
hey @mss- try (ns myproject.core (:import path.to.class))
no, it's the full class name
no- the lein declaration is something different
the dependencies declaration tells lein where to find the jar file containing the library
the import tells Clojure where to find the class file you want to use (which is in the library)
so something like (ns abc (:import java.lang.String))
or to get a bunch of classes from the same package (ns abc (:import [java.lang String Object Integer]))
so what I think is tripping me up is – for a third party lib, where can I find out what the equivalent of java.lang
in your example is for my installed third party lib
however, you can always refer to Java classes with the full class name (even without import)
well, that's a matter of looking at the lib's api
the only thing import does is let you refer to a class by it's short name (String) instead of it's full name (java.lang.String)
which is helpful for readability (and your own sanity), but also somewhat for documentation purposes at the top of the file
you can peek inside jar files pretty easily at the command line with jar tf the.jar
(it's really just a zip file of classes)
that makes sense. so maybe this is my poor understanding of java showing through, but if this third-party lib exports a public class called SomeClass
, all I would need to do is (ns my-project (:import SomeClass))
?
technically you don't even need to do that
you can just refer to SomeClass in your code
the only benefit of import
is to let you use shorter class names (no package)
like you can do (java.util.Date.)
anywhere without importing it
yes, there is no difference
from Java/Clojure's perspective, there are just a giant tree of available classes
when you need one, it will be loaded
import
is only about changing what names you can use to refer to those
in the scope of a particular namespace
that makes a lot of sense. thanks for the clarification – must just be instantiating the thing incorrectly
the best place to try stuff out is of course the repl :)
the full name of a class should evaluate to its Class object - that can tell you if you have the right name or not
user=> java.util.Date
java.util.Date
user=> java.util.Dte ;; oh no!
CompilerException java.lang.ClassNotFoundException: java.util.Dte
so that’s the tricky part to me. java.util.date
obv resolves correctly, and prints out the class object. my third party SomeClass
when I try to the send it to the repl throws an Unable to resolve symbol
error back at me
and poking through the source of this library, the only public class is SomeClass
, so I’m confused why it wouldn’t resolve correctly
usually Java classes are in a package