12

I have some values repeated in my project.clj like below:

(defproject foo "0.1.0-SNAPSHOT"
  ...

  :dependencies [[bar "3.4.5"]
                 [baz "3.4.5"]
                 [bat "3.4.5"]]

  ...)

I would like to use some sort of constant here so, when these libraries get updated to 3.5.0 for example, I have to change just one place.

What is the best practise here? Best I can come up with is this:

(def deps-version "3.4.5")

(defproject foo "0.1.0-SNAPSHOT"
  ...

  :dependencies [[bar ~deps-version]
                 [baz ~deps-version]
                 [bat ~deps-version]]

  ...)
5
  • I added an answer but thought you were unable to get the constants to work, is this the case? You're missing the ~ char in your example, i.e. [bar ~deps-version]. Your own solution is something I've used outside of leiningen (in gradle for selenium driver versions) so there's only one change to make, and fits well enough. Jun 29, 2015 at 12:46
  • 1
    so what's the essence of your question now? if it's just "should I use versions like this?" then answer's probably simply, yes. Jun 29, 2015 at 21:48
  • @MarkFisher, muhuk is asking what the best practice is. You have stated what you believe the best practice is, so you could offer it as an Answer.
    – Mars
    Jun 30, 2015 at 6:02
  • which seems a little subjective to me as a 'question' Jun 30, 2015 at 6:05
  • @MarkFisher you are right, I probably didn't phrase my question right. But I was wondering if there were other, better ways. To give a somewhat contrived example, you can do #=(eval (...)) and read constants from a file. (Which is perhaps not too absurd since you can make this file a resource and read it within the app too.) Feel free to edit the question BTW, the gist is how to define constants.
    – muhuk
    Jun 30, 2015 at 9:14

1 Answer 1

6

Using a definition before defproject is totally fine and common practice.

You can also so more advanced stuff like depending on the build environment (I would tag the effective project version too that case). A nice example is found in the answers to this question "Leiningen: How to define constants in project.clj?"

1
  • 2
    The link, uh, leads to this same page. To understand recursion one must understand recursion...
    – D-side
    Aug 19, 2015 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.