0
votes

I am wondering what magic is occurring for this code from https://github.com/weavejester/compojure/wiki/Destructuring-Syntax

(GET "/" request (str request))

I would expect it to look something like

(GET "/" [request] (str request))

Specifically, how is the request bound to the second argument?

Is the request always bound to the second argument?

What differentiates request vs [request]?

I have changed my code to see what happens in each of these scenarios, just trying to understand the reason and make sure I don't make and wrong assumptions about the second arg being bound to the request.

Thanks -jv

1

1 Answers

2
votes

The request map is always bound to the second argument passed to the route macros. If you bind it as map, it will be destructored via regular Clojure Map binding destructoring. If you bind it as a vector, Compojures macro looks the symbols up as equally named keys in the :params map of the request map. The latter is Compojure specific and explained in the link you provided with the question.

The binding takes place by the GET macro transforming the forms you pass to it into sourcecode of a request handler function with the desired lookups in scope.

The first example binds request to the entire request map.

The second example binds request to the value of key :request of the map of key :params of the request map.