本文介绍了嵌入式swank-clojure repl是否可以访问它嵌入的程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图嵌入一个swank-clojure的副本到我的应用程序,以连接的目的,而应用程序运行到垃圾的东西。



嵌入swank-clojure REPL非常简单:

 (ns mytest 
(:use [swank.swank:exclude [-main]])
(:gen- ))

(defn -main [& args]
(let [x 123]
(swank.swank / start-repl)))



运行程序..然后在emacs中:

  Mx slime-connect 

现在,我希望是这将工作:

 (println x)
;; 123(我所希望的)
;;无法解析符号:x在这种情况下(残酷的现实)


嵌入式REPL与我正在运行的程序进行交互的方式是什么?



如果没有,有什么原因嵌入一个REPL?



如果有什么不同,我试图作为一个JAR运行。 / p>

这个线程似乎相关,但我不能从中获得:



解决方案

- 对局部变量是词法作用域,因此 swank.swank / start-repl 不会受到调用它的 let 形式的影响。但是,运行的REPL将能够 require / 使用任何Clojure命名空间在应用程序的类路径c $ c> in-ns 将REPL的命名空间切换到其中一个)和 import 在类路径上的任何Java类,做一些非常有用的事情,如重新定义函数,检查和更改任何参考/原子/其他感兴趣的东西在Vars,调用函数/ Java方法等的内容。



请注意,您可能不应在 ns 表单中(:use swank.swank) (:require swank.swank)。区别在于前者将所有 swank.swank 的public Vars加入到您的命名空间中,而后者不会( use = require +请参阅,有关详细信息,请参阅(doc use)等。你似乎使用命名空间限定的符号来访问Swank的Vars,所以你可能甚至不需要改变剩下的代码, require 可以避免混淆你的命名空间。或者,(:use [swank.swank:only [start-repl]]);这拉入了 start-repl Var,你可以直接使用,而不用 swank.swank / 位。


I'm trying to embed a swank-clojure repl into my application for the purpose of connecting while the app is running to muck around with things. However, I'm having trouble figuring out how to access the enclosing environment after starting the repl.

Embedding the swank-clojure REPL is easy enough:

(ns mytest
  (:use [swank.swank :exclude [-main]])
  (:gen-class))

(defn -main [& args]
  (let [x 123]
    (swank.swank/start-repl)))

Run the program.. then over in emacs:

M-x slime-connect

That works fine and I am connected. Now, what I hoped was that this would work:

(println x)
;; 123 (what I was hoping for)
;; Unable to resolve symbol: x in this context (cruel reality)

So that doesn't work as a way to pass the current environment to the embedded REPL.

Is there any way for the embedded REPL to interact with my running program?

If not, what reasons are there to embed a REPL?

If it makes any difference, I am trying to run this as a JAR.

This thread seems related, but I wasn't able to get anywhere from it:

Embedding swank-clojure in java program

解决方案

let-bound locals are lexically scoped, thus swank.swank/start-repl won't be affected by a let form wrapped around the call to it. However, the running REPL will be able to require / use any Clojure namespaces on your application's classpath (or use in-ns to switch the REPL's namespace to one of those) and to import any Java classes on the classpath, allowing you to do a number of very useful things, such as redefining functions, examining and changing the contents of any Refs / Atoms / other things of interest held in Vars, calling functions / Java methods etc.

Note that you probably shouldn't (:use swank.swank) in your ns form; (:require swank.swank) instead. The difference is that the former pulls in all swank.swank's public Vars into your namespace, while the latter doesn't (use = require + refer, see (doc use) etc. for details). You seem to use namespace-qualified symbols to access Swank's Vars, so you might not even have to change the rest of your code, and require avoids cluttering up your namespace. Alternatively, (:use [swank.swank :only [start-repl]]); this pulls in just the start-repl Var, which you could then use directly, without the swank.swank/ bit.

这篇关于嵌入式swank-clojure repl是否可以访问它嵌入的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 00:17