Search code examples
clojureitextclojure-java-interop

Clojure, same method call on different Java Objects


In this code headerTable and rowsTable are Java Objects. Here the same method with the same argument is being called on them:

(.setHorizontalAlignment headerTable Element/ALIGN_LEFT)
(.setHorizontalAlignment rowsTable Element/ALIGN_LEFT)  

Is there a better way of doing this? I would think there must be a way to combine the two calls into one somehow. But since this is 'side effecting' code, perhaps not??

I'm thinking of an answer without writing a custom function or macro, something like "just use juxt or comp", but then maybe I'm being a bit too prescriptive...

Edit Type hinting was mentioned by Leonid Beschastny, so just in case it helps, here's the Java method signature:

public void setHorizontalAlignment(int horizontalAlignment)  

And the class is PdfPTable, from iText. (This code is being used to create PDF files).


Solution

  • There are many possible refactorings, one would be

    (run! #(.setHorizontalAlignment ^PdfPTable % Element/ALIGN_LEFT)
          [headerTable rowsTable])