Using imported Java libs in Clojure REPL

安稳与你 提交于 2020-03-22 09:04:39

问题


Twilio docs for Java lib

MVN for this lib

I'm trying to use Twilio from Clojure. Pretty new to Clojure dev, so I'm trying to get to grips with importing libs, in general.

My project.clj looks like this:

(defproject magical-new-project "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
            :url "https://www.eclipse.org/legal/epl-2.0/"}
  :dependencies [[org.clojure/clojure "1.10.0"]
                 [com.twilio.sdk/twilio "7.47.5"]]
  :repl-options {:init-ns magical-new-project.core})

I think I'm correctly importing the Twilio Java lib, but I'm not sure.

My core.clj looks like:

(ns magical-new-project.core
  (:import [com.twilio Twilio]
           ;[com.twilio.http Request Response]
           [com.twilio.rest.api.v2010.account Message]
           [com.twilio.rest.api.v2010.account Call]
           [com.twilio.type PhoneNumber]))

(defn foo
  "I don't do a whole lot."
  [x]
  (println x "Peacefully disengage, World!"))

As far as I can tell, this should be correctly importing the Twilio class shown here.

So then I try (from REPL) to initialize the Twilio object that I hope I have successfully imported, but it fails.

$ lein repl
nREPL server started on port 62356 on host 127.0.0.1 - nrepl://127.0.0.1:62356
REPL-y 0.4.3, nREPL 0.6.0
Clojure 1.10.0
OpenJDK 64-Bit Server VM 12.0.1+12
    Docs: (doc function-name-here)
          (find-doc "part-of-name-here")
  Source: (source function-name-here)
 Javadoc: (javadoc java-object-or-class-here)
    Exit: Control+D or (exit) or (quit)
 Results: Stored in vars *1, *2, *3, an exception in *e

magical-new-project.core=> (Twilio. "My API or Account SID goes here" "My AUTH_TOKEN or API secret goes here")
Syntax error (IllegalArgumentException) compiling new at (form-init14687807219308370487.clj:1:1).
No matching ctor found for class com.twilio.Twilio

As far as I can tell, the No matching ctor thing means there is no constructor function that accepts the arguments I'm presenting, but line 39 of the Twilio.java file seems to take two strings, and then if you look here, you'll see that I'm sending the right arguments (the ACCOUNT_SID and the AUTH_TOKEN).

At the moment, I'm not sure whether I'm (1) correctly importing the Twilio class, (2) adding the dependencies to the project correctly, (2) correctly using the REPL, or (4) correctly using the Twilio SDK. Maybe I'm doing all of these incorrectly.

I would really appreciate any help or advice that you could afford me.


回答1:


the function you linked to is not a constructor but a static function on the class. You call a static function on a class in clojure like (Twilio/init "foo" "bar"). Your syntax is correct if it were actually a constructor.

Thanks @juraj. I wasn't sure if this actually qualified as an answer :-)



来源:https://stackoverflow.com/questions/60535813/using-imported-java-libs-in-clojure-repl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!