Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6346323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:59:43+00:00 2026-05-24T20:59:43+00:00

I understand that the binding form allows rebindable dynamic scoping in clojure. So far

  • 0

I understand that the binding form allows rebindable dynamic scoping in clojure. So far the only uses I’ve seen it used for is for I/O such as with print where *out* is rebound to what ever writer you would like at the time.

I would like to see examples that truly take advantage of the power of binding where other facilities really don’t work. Personally I’ve only used it in cases where passing around a user supplied object to all the functions was really tedious. Basically a situation where I am trying to create a context that the helper functions uses. (Similar to this case When should one use the temporarily-rebind-a-special-var idiom in Clojure? ) To be more specific, I was relying on the user to create a dynamic binding to the *db* var to allow the database functions to know what to operate on. This was particularly useful when the user needs to write lots of nested calls to the database functions. Typically, I’m OK if I need to write macros to make things easier for myself, but to require the user to do so seems bad. That being said, I try to avoid doing so as much as possible.

What are some other good use cases for ‘binding’ that I can copy and incorporate into my code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T20:59:44+00:00Added an answer on May 24, 2026 at 8:59 pm

    I use bindings for two reasons:

    1. running tests that override constants or other values of other symbols
    2. using “global” resources such as database connections or message broker channels

    testing

    I am working on a distributed system with several components that communicate by sending messages over message exchanges. These exchanges have global names, which I have defined like such:

    (ns const)
    (def JOB-EXCHANGE    "my.job.xchg")
    (def CRUNCH-EXCHANGE "my.crunch.xchg")
    ;; ... more constants
    

    These constants are used in a number of places to send messages to the right place. To test my code, part of my test suite runs code that uses the actual message exchanges. However, I don’t want my testing to interfere with the actual system.

    To solve this, I wrap my testing code in a binding call that overrides these constants:

    ;; in my testing code:
    (binding [const/CRUNCH-EXCHANGE (str const/CRUNCH-EXCHANGE (gensym "-TEST-"))
              const/CRUNCH-TASK-QUEUE (str const/CRUNCH-TASK-QUEUE (gensym "-TEST-"))]
      ;; tests here
    )
    

    Inside of this binding function, I can call any code that uses the constants and it’ll use the overridden values.

    using global resources

    Another way I use bindings is to “fix” the value of a global or singleton resource inside a particular scope. Here’s an example of a RabbitMQ library I wrote, where I bind the value of a RabbitMQ Connection to the symbol *amqp-connection* so that my code can use it:

    (with-connection (make-connection opts)
      ;; code that uses a RabbitMQ connection
    )
    

    The implementation of with-connection is quite simple:

    (def ^{:dynamic true} *amqp-connection* nil)
    
    (defmacro with-connection
      "Binds connection to a value you can retrieve
       with (current-connection) within body."
      [conn & body]
      `(binding [*amqp-connection* ~conn]
         ~@body))
    

    Any code in my RabbitMQ library can use the connection in *amqp-connection* and assume that it is a valid, open Connection. Or use the (current-connection) function, which throws a descriptive exception when you forgot to wrap your RabbitMQ calls in a with-connection:

    (defn current-connection
      "If used within (with-connection conn ...),
       returns the currently bound connection."
      []
      (if (current-connection?)
        *amqp-connection*
        (throw (RuntimeException.
          "No current connection. Use (with-connection conn ...) to bind a connection."))))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand that Microsoft uses this template when versioning their products: Major.Minor.Build.Revision. Major is
I'm new to WPF data binding. I have a ListBox on a form that
I understand that some countries have laws regarding website accessibility. In general, what are
I understand that there are several ways to blend XNA and WPF within the
I understand that they are both supposed to be small, but what are the
I understand that server-side validation is an absolute must to prevent malicious users (or
I understand that IronPython is an implementation of Python on the .NET platform just
I understand that an id must be unique within an HTML/XHTML page. For a
I understand that these methods are for pickling/unpickling and have no relation to the
I understand that requests are served by different threads, but do they all come

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.