Fork me on GitHub
#clojure
<
2018-07-15
>
henrik10:07:39

I’m trying to call Google Cloud Language (sentiment analysis, categorization service) from Clojure, using their offical Java client. The Java tutorial says,

// Imports the Google Cloud client library
import com.google.cloud.language.v1.Document;
import com.google.cloud.language.v1.Document.Type;
import com.google.cloud.language.v1.LanguageServiceClient;
import com.google.cloud.language.v1.Sentiment;
I’ve translated this to
(ns exp.language
  (:import [com.google.cloud.language.v1
            Document
            LanguageServiceClient
            Sentiment]))
But Document.Type eludes me. Putting Document.Type among the imports leads to a ClassNotFoundException. What is Type, and how do I import it?

mitranim11:07:20

@henrik Most likely Document$$Type

henrik11:07:46

Document$Type worked, thank you! So, a nested class according to Java interop documentation.

mitranim11:07:03

Yeah. This exposes them for what they are: fake compiler trickery 🙂

😈 4
dottedmag22:07:56

Is there anything in Clojure standard library similar to with-open, but for an arbitrary value (say, implementing a particular interface)?

dottedmag22:07:04

I don't see anything like that in src/clj/clojure/core.clj though.

dottedmag22:07:21

Actually I'm trying to do a multi-step construction of a stateful component, where every step should be rolled back if the next one fails. I have come up with the following monstrous construction, is there way to make it better?

(defn open []
    (let [a (open-a)]
        (try
            (let [b (open-b a)]
                (try
                    (let [c (open-c a b)]
                        {:a a :b b :c c})
                    (catch Exception e
                        (close {:a a :b b})
                        (throw e)))
            (catch Exception e
                (close {:a a})
                (throw e)))))

(defn close [{:keys [a b c]}]
    (when c
        (close-c c))
    (when b
        (close-b b))
    (when a
        (close-a a)))

;; usage
(let [session (open)]
    (try
        (do-some-stuff session)
        (finally
            (close session))))

john23:07:54

They describe making components Closeable for usage with with-open