Fork me on GitHub

Hello, is there a way to do redirection for GET/POST requests with ring? Like I want to be able to temporarily preserve backwards compatibility such that GET/POST to /_crux/query to become GET/POST in /_xtdb/query . I know there is ring.util.response/redirect that doesn't seem to help for when it is a query. And I quite seem to get the handler needed unless I am to modify the library


{:status 302 :headers {"Location" "/_xtdb/query"}}


Thanks! That pointed me to the right direction. Trying to figure out the body now

👍 1

Actually. After tinkering with it, I think in this method I still need to have the handler fn


(ring.util.response/redirect "/_xtdb/query) was just producing {:status 302 :headers {"Location" "/_xtdb/query" :body ""}} which isn't surprising i guess that's how ring works


HTTP 307 is a, but wouldn't help if clients don't handle it. How about just hooking up _xtdb/query's handler to the _crux/query route, or calling the former from the latter's handler? I.e. accept the request and handle the backwards compatibility server-side.


Honest question: We tend to prefer requiring stuff with :as over using :refer or use. Why?


To easily know where a function is coming from. This is why I like using import * as _ for lodash, so I know exactly where a function lives just by looking at its use

👍 2

@U04V5VAUN it makes reading code without using an IDE or having 20 tabs open so much easier

❤️ 2
Alex Miller (Clojure team)13:03:07

both remove context from the function at the point of use

Alex Miller (Clojure team)13:03:21

if you just see foo ,you have no indication of whether that's in the current namespace or from elsewhere (and with use, you really need some level of static analysis to even tell where it came from). with refer you at least have a list at the top of your file

Alex Miller (Clojure team)13:03:24

for functions/macros acting as syntax, this removal of context can actually be a feature (like refer'ing >! from core.async or a defwhatever macro)


Namespace prefixes aide me in both reading and writing code. Knowing where a function comes from at a glance is so nice. I really miss this every time i take another stab at using a scheme or other lisp. For writing, the prefix helps for autocomplete so much. And I just recently felt the pain of use when a var with the same name was in both namespaces. I copied a var from the test ns and put it into the main namespace, and then later went into the test namespace. Since it used :refer :all on the original namespace, I had a collision of var names. Super annoying. And this pitfall exists everywhere you use use. Changing one namespace could break consumers anywhere. It is a nightmare


(I think that macro as syntax heuristic is really tasteful. Gonna incorporate that)

Alex Miller (Clojure team)14:03:03

a little bit goes a long way :)


I never get dogmatic in my style. And its a good rule of thumb for how to decide when to use refer. So thanks 🙂


use CMD-Shift-Enter to post a snippet


that’s an enormous block of text and I think you are asking for help making javascript code, which we aren’t really experts at here.


Interesting discussion about identifying special forms in editor for highlighting, #{let fn letfn loop} all report :special-form true in their metadata despite not being special forms and not being in (clojure.lang.Compiler/specials).

🙏 1

if it has metadata it isn't technically a special form


special forms have no vars to hang metadata on


macros and special forms behave differently in regard to shadowing


agree. so it sounds like anything reporting to be :special-form is lying almost by definition


special forms basically define an interface between the language and the compiler and it is not surprising that you might want some kind of facade over that, and macros are an attractive way to implement that facade


but yeah, you cannot substitute a macro for a special form and have it work the same in all situations


and you cannot really hide the "real" special forms anyway


yeah. i think this is just for syntax highlighting. and someone pointed out some things were both a macro and a special form which caused a double take leading us here


i don’t believe any of this is semantic and just started with thrown being highlighted as a macro.


it is a whole thing and a half


"special form" is not some much of a prescriptively defined notion, but something that just kind of arises out of writing a lisp with macros, the macros have to bottom out at some base language, and that base language needs to be understood by eval

Sam Ritchie18:03:33

Hey all; does anyone know how I can set a namespace, if I am calling into the clojure compiler from java? I am trying

Compiler.eval (RT.readString ("(in-ns 'user)"));
and seeing
Exception in thread "main" java.lang.IllegalStateException: Can't change/establish root binding of: *ns* with set


Don't remember all the details, but it requires some wiring around it:


my vague notion here is that *ns* is something that's designed to be part of a REPL context, and Compiler.eval is lower level, and doesn't have the kind of state tracking / session that a REPL provides and `*ns* is useful in

Sam Ritchie18:03:03

this came up because java was trying to call some clojure that, internally, ran (eval '(clojure.core/+ 2 3)), and threw a

Caused by: java.lang.RuntimeException: No such var: clojure.core/+

Sam Ritchie18:03:54

in this case it was because *ns* is clojure.core by default, and the java code first evaluated a require form that replaced the + definition

Sam Ritchie18:03:05

so my goal here is to change namespaces FIRST to user or something. checking that link now


yeah, *ns* isn't bound, so you have to bind it yourself


try  {
} finally {


I think ideally you wouldn't use Compiler.eval, RT.readString, or Var.* whatever, but would use the java api ( to get a handle on clojure.core functions that do those things

Sam Ritchie18:03:45

hey, nice, that is better. I did get this working but let me give this a try


Using java.api.Clojure works only if you need to do something simple, like call a known function with known arguments. Without eval, you can't do even (+ 1 2).


eval is a function in clojure.core


And... calling it on (+ 1 2) will not complain about + being missing?


depends on the value of *ns*


Which you will need to bind, thus going to the very start of the discussion? :)


user=> (.invoke ( "clojure.core/eval")
         ( "(+ 1 2)"))



Alright, then I was doing something wrong during my previous experiments... Thanks!


user=> (try
  (.invoke ( "clojure.core/push-thread-bindings")
           (.invoke ( "clojure.core/hash-map")
                    (.invoke ( "clojure.core/resolve")
                             (.invoke ( "clojure.core/symbol")
                    (.invoke ( "clojure.core/create-ns")
                             (.invoke ( "clojure.core/symbol")
  (.invoke ( "clojure.core/eval")
         ( "(+ 1 2)"))

    (.invoke ( "clojure.core/pop-thread-bindings"))))
Syntax error compiling at (REPL:1:1).
Unable to resolve symbol: + in this context

Sam Ritchie18:03:40

my problem is that I was importing sicmutils.env which knocked out + from clojure.core., since *ns* was bound to clojure.core by default

Sam Ritchie18:03:10

so when I THEN tried to do (clojure.core/+ 1 2) from java, clojure.core/+ had not just been shadowed but basically knocked out

Sam Ritchie18:03:23

// Needed to allow creating new namespaces.
    Var.pushThreadBindings(RT.mapUniqueKeys(RT.CURRENT_NS, RT.CURRENT_NS.deref()));
    try {
      Compiler.eval("(ns user" +
              "(:refer-clojure :exclude [partial zero? + - * / ref compare = numerator denominator])" +
              "(:require [pooty :as p] [sicmutils.env :refer :all]))"));
      Compiler.eval("(println \"in\" (ns-name *ns*)))"));
      System.out.println(Compiler.eval (RT.readString ("(clojure.core/+ 1 2)")));
                              ("(->infix (((exp D) (literal-function 'eff)) 'x))")));
    } finally {


Ah, no - I was evaluating different code. Eval'ing (ns user) doesn't work, because it still requires *ns* to be bound.

Sam Ritchie18:03:32

but, VERY annoyingly, :refer-clojure :exclude does not silence warnings here

Sam Ritchie18:03:38

WARNING: = already refers to: #'clojure.core/= in namespace: user, being replaced by: #'sicmutils.env/=

Sam Ritchie18:03:01

ignore the space in that snippet!!! warnings still occur. edit, fixed.


@U2FRKM4TW my next example shows how to use to setup binding for *ns*


it is slightly incorrect, because I think you want the push outside of the try/catch, but I am too lazy to add the extra do to make it a nice single form for copying and pasting into the repl


Makes sense! So in the end, my Java version on SO seems to be doing the same thing but via a less ideal API.


and, despite commonly referring to special forms by the symbol that is the first part of the list, it is the whole list that is the special form (why it is a special form and not a special symbol)


*ns* is basically "compilation context", it is used when compiling forms to resolve symbols, so if you are outside of a place where compilation is known to be happening (the repl, loading a file) then it won't be bound and not settable

💯 2

you can bind it yourself


the same way the compiler does var binding


for syntax highlighting the best thing to do is come up with your own name "most favored symbols" and then make those whatever you want, and it will always be correctly because it is your name and you define it


@hiredman The issue is a bit more general than syntax highlighting: the clj-kondo static analyzer emits information that other tools (like clojure-lsp) use. Should clj-kondo say in its analysis that when clojure.core/fn is called, that this was a special form invocation or not? The medadata :special-form makes this a bit blurry.


Perhaps it should follow what tools like tools.analyzer do, I haven't checked there, but I expect that it only says "special form" for special-symbol? stuff


Hmm, no it seems tools.analyzer doesn't add that info


Yes, that thing isn't really used in the output


each special form maps to a unique node type


ugh, sorry, just noticed how old this was. my slack was jumping around


But I would say it's the "correct" answer, and the other things are just macros


but like catch isn't a special form


(try ...) is a special form and catch is part of its dsl


user=> (let [catch identity] (catch 1))


Agreed. It also doesn't behave like a special symbol here:

user=> (let [catch (fn [] :foo)] (catch))


@ericdallo In any case, you can use special-symbol? + the clojure.core namespace to determine yourself in clojure-lsp if it's a special form.


Should we add this to clj-kondo? that idea of :special true


user=> (clojure.set/difference cljs-specials clj-specials)
#{defrecord* ns* ns js*}
user=> (clojure.set/difference clj-specials cljs-specials)
#{monitor-exit reify* clojure.core/import* monitor-enter}