This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-12-21
Channels
- # adventofcode (47)
- # aleph (18)
- # announcements (20)
- # babashka (81)
- # beginners (23)
- # biff (6)
- # calva (5)
- # cider (50)
- # clojure (34)
- # clojure-europe (19)
- # clojure-norway (11)
- # clojure-spec (6)
- # clojure-uk (1)
- # clojurescript (2)
- # conjure (2)
- # cursive (14)
- # datomic (1)
- # humbleui (11)
- # hyperfiddle (3)
- # introduce-yourself (5)
- # joyride (1)
- # nbb (7)
- # off-topic (19)
- # podcasts-discuss (1)
- # reagent (3)
- # reitit (19)
- # releases (1)
- # ring-swagger (1)
- # shadow-cljs (29)
- # sql (6)
- # squint (56)
Hello everyone. Slipway by Factor House is now GA and published to Clojars: https://github.com/factorhouse/slipway
I'm not much of a regular on this slack but I'll check back in this thread later on to answer questions if there are any. Ta.
How does this companionship work? The README doesn't describe anything useful in one sentence. So is it a configuration helper or is it some kind of a separate UI tool? There is a UI so why do you need it if it's a config helper? Is it a UI server configuration tool? Is there a simple way to understand what it accomplishes?
Also this post should've probably be posted into #announcements
instead.
🎄 🎁 Merry Christmas everybody! I made you a present: a nestable version of #()
anonymous functions.
https://github.com/opqdonut/hash-f
Hi guys, question, I have multiple records, that implement a protocol, but the implementation of each protocol method is basically the same for every record that implements it (might vary one or more form record to record). That said, the records have values passed in the constructor, values which are needed inside the implementation of each method. I know this might sound crazy, but is there a way to reference those values in a way where they are only defined in runtime? Macros are expanded in compile time, so that wouldn’t work I guess. Thoughts? Example:
(defprotocol TestProtocol
(getFile [this])
(getName [this]))
(defrecord TestRecord [conf])
(def default-implementation
{:getFile (fn [this] (:file conf)
:getName (fn [this] (:name conf)})
(extend TestRecord TestProtocol default-implementation)
You can write (.-conf this)
to access the conf
field of a record in the default-implementation
map.
I am going to try that out, thanks!
(defprotocol TestProtocol
(getFile [this])
(getName [this]))
(defrecord TestRecord [conf])
(def default-implementation
{:getFile (fn [this] (:file (.-conf this)))
:getName (fn [this] (:name (.-conf this)))})
(extend TestRecord TestProtocol default-implementation)
(getFile (TestRecord. {:file "filename" :name "Bob Dabolina"})) ;; => "filename"
It works! Do you have any idea how and if reflection can be avoided here?
Btw, same guy, different username idk why
I might need to add type hint or just use a keywordized version of the field name, it should work as well
@U0P0TMEFJ thanks for your help, it worked and I am now able to do what I wanted 🙂
Just doing (:conf this)
didn't work? It should work, and it is preferable over using the interop syntax when working with records.
Like this should work:
(def default-implementation
{:getFile (fn [this] (-> this :conf :file))
:getName (fn [this] (-> this :conf :name))})
this
refers to the record, and you can use it like you would use the record anywhere, which is normally to treat it like a map.
This is a somewhat contrived example but there is a curious behavior that may or may not be a bug. Suppose you have the following protocol:
(defprotocol P (f [this]))
And concrete type with a mutable parameter and lock:
(deftype T [^:volatile-mutable x lock]
P
(f [this]
(locking lock
(set! x true)
(prn ""))))
Compiler has no problem with this. But move the (prn "")
statement outside the (locking …)
block and you get:
(deftype T [^:volatile-mutable x lock]
P
(f [this]
(locking lock
(set! x true))
(prn "")))
Syntax error (IllegalArgumentException) compiling fn* at (REPL:4:5).
Cannot assign to non-mutable: x
@dmiller spotted this behavior in the lastest ClojureCLR and is sleuthing it on his end but the fact the same error is raised in JVM Clojure suggests that either there is a bug in Clojure proper, or some highly counterintuitive rule is at work here, the details of which are probably worth understanding. Why the compiler would view x as non-mutable in the 2nd example is not clear. Any clarification appreciated.probably a bug - put it on http://ask.clojure.org
I know we have https://clojure.atlassian.net/browse/CLJ-2126 in the 1.12 list which is in this ballpark (but kind of the opposite)
I added it at http://ask.clojure.org and referred to David's bug report at ClojureCLR atlassian: https://clojure.atlassian.net/browse/CLJCLR-122
my guess is that this is a by-product of https://clojure.atlassian.net/browse/CLJ-1472
The compiler will often hoist things like try/catch into immediately invoked anonymous functions, easiest way to give expression like semantics to more complex bytecode
I'm quite relieved to learn that CLJCLR-122 is also a problem over on the JVM, in the hope that someone else will solve the problem before I have to. (The immediate locus of the problem is at the ClojureCLr equivalent to https://github.com/clojure/clojure/blob/527b330045ef35b47a968d80ed3dc4999cfa2623/src/jvm/clojure/lang/Compiler.java#L4943 , but the most obvious fix at that location does not work.) (In contrast, CLJ-2126 is not a problem for ClojureCLR; I think this is called a 'happy accident'.)
It turns out that try
is sufficient, even without a finally
.
(deftype T [^:volatile-mutable x]
P
(f []
(try
(set! x 7)
(prn ""))))
succeeds.
(deftype T [^:volatile-mutable x]
P
(f []
(try
(set! x 7))
(prn ""))))
fails.I tracked down the cause. Comment added to the CLJ-2743. The cause was code added in 2008.
I just ran into this using binding
too https://clojurians.slack.com/archives/C03S1KBA2/p1672167142121029