在Clojure REPL中使用导入的Java库 [英] Using imported Java libs in Clojure REPL

查看:149
本文介绍了在Clojure REPL中使用导入的Java库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用于Java库的Twilio文档

此lib的MVN

我正在尝试使用Clojure的Twilio。 Clojure开发人员还很陌生,因此,一般来说,我想尽一切办法来导入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.

我的 project.clj 看起来像这样:

(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})

我想我是正确导入Twilio Java库,但是我不确定。

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

我的 core.clj 看起来像:

(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!"))

据我所知,这应该正确导入此处显示的Twilio类。

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

因此,我尝试(从REPL)初始化希望已成功导入的Twilio对象,但失败。

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

据我所知,没有匹配的ctor 表示没有构造函数接受我要提供的参数,但是 Twilio.java文件的第39行似乎采用了两个字符串,并且那么,如果您看这里,您会看到我正在发送正确的参数(ACCOUNT_SID和AUTH_TOKEN)。

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).

目前,我不确定我是否(1)正确导入了Twilio类,(2)正确将依赖项添加到项目,(2)使用REPL正确地或(4)使用Twilio SDK正确地。也许我做错了所有这些。

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.

推荐答案

链接到的函数不是构造函数,而是类上的静态函数。您可以在Clojure中的类上调用静态函数,例如(Twilio / init foo bar)。如果它实际上是构造函数,则语法是正确的。

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.

感谢@juraj。我不确定这是否真的可以作为答案:-)

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

这篇关于在Clojure REPL中使用导入的Java库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆