im trying to implement this in java
window.addWindowListener(new WindowAdapter() {
#Override
public void windowDestroyNotify(WindowEvent arg0){
new Thread(){
#Override
public void run(){
animator.stop();
System.exit(0);
}
}.start();
};
});
as this in clojure
(.addWindowListener (proxy [WindowAdapter][]
(windowDestroyNotify [arg0]
(.start (proxy [java.lang.Thread][]
(run
(.stop ani)
(System/exit 0)))))))
but when i run it, it gives me this error
CompilerException java.lang.IllegalArgumentException: Don't know how to create ISeq from: clojure.lang.Symbol, compiling: (program/core.clj:36:36)
36:36 is where "(proxy [java.lang.Thread]" starts
why can't it implement java.lang.Thread
The exception is misleading, but the problem is in the implementation of the run method in the proxy Thread, it is missing the arguments vector.
The following expression when compiled generates the same exception:
(proxy [Thread] [] (run (inc 1)))
While this one doesn't:
(proxy [Thread] [] (run [] (inc 1)))
The misleading error is because of how the proxy macro parses its arguments.
Start over and build it up slowly from small pieces:
(let [t (Thread. #(println "going 1")) ]
(.start t))
;=> going 1
(let [t (proxy [Thread] []
(run [] (println "going 2")))
]
(.start t))
;=> going 2
So you can see the problem is not proxy or Thread but in your surrounding code.
Update
Juan noticed the problem first as the missing arglist [] for run. By reading the docs at https://clojuredocs.org/clojure.core/proxy and building up the sample slowely, I got the arglist in the sample code automatically.
I have found that understanding a problem by "synthesis" allows you to bypass many problems. It is often much faster & easier than trying to understand a problem by "analysis", where you start with the finished product and then try to figure out what caused it to go wrong.
Related
I am preparing an R wrapper for a java code that I didn't write myself (and in fact I don't know java). I am trying to use rJava for the first time and I am struggling to get the .jcall right.
Here is an extract of the java code for which I write a wrapper:
public class Model4R{
[...cut...]
public String[][] runModel(String dir, String initFileName, String[] variableNames, int numSims) throws Exception {
[...cut...]
dir and initFileName are character strings for the directory and file name with initial conditions, variable names is a list of character strings that I would write like this in R: c("var1", "var2", "var3", ...) and can be of length from one to five. Finally, numSim is an integer.
Here is my tentative R code for a wrapper function:
runmodel <- function(dir, inFile, varNames, numSim){
hjw <- .jnew("Model4R")
out <- .jcall(hjw, "[[Ljava/lang/String", "runModel", as.character(dir), as.character(inFile), as.vector(varNames), as.integer(numSim))
return(out)
}
The error in R is:
Error in .jcall(hjw, "[[Ljava/lang/String", "runModel", as.character(dir),
: method runModel with signature (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;II)[[Ljava/lang/String not found
I suspect that the JNI type isn't correct for String[][]. Anyhow, any help that could direct me towards a solution would be welcome!
You're missing a semicolon at the end of the JNI for String[][] - it should be "[[Ljava/lang/String;". Also, I think you need to call .jarray instead of as.vector on varNames. The R error is telling you that rJava thinks the class of the third argument is Ljava/lang/String; instead of [Ljava/lang/String;.
I am doing a small project it is a interoperability of java and .net dll.
Focus:
I just have a java file which calls the .net dll which is created by using C# and CPP and MCPP..
The program is just a hello world Program..
I just refer the below mentioned sites.
http://www.codeproject.com/Articles/378826/How-to-wrap-a-Csharp-library-for-use-in-Java
http://www.codeproject.com/Articles/13093/C-method-calls-within-a-Java-program
Finally I just get some ideas and finally did this with some errors.
Coding:
#using "mscorlib.dll"
#using "CSharpHelloWorld.netmodule"
using namespace System;
public __gc class HelloWorldC
{
public:
// Provide .NET interop and garbage collecting to the pointer.
CSharpHelloWorld __gc *t;
HelloWorldC() {
t = new CSharpHelloWorld();
// Assign the reference a new instance of the object
}
// This inline function is called from the C++ Code
void callCSharpHelloWorld() {
t->displayHelloWorld();
}
};
Errors:
Error 1 error C4980: '__gc' : use of this keyword requires /clr:oldSyntax command line option
Error 2 error C3699: 'interior_ptr' : cannot use this indirection on type 'CSharpHelloWorld'
Error 3 error C2750: 'CSharpHelloWorld' : cannot use 'new' on the reference type; use 'gcnew' instead
Error 4 error C2440: '=' : cannot convert from 'CSharpHelloWorld *' to 'CSharpHelloWorld ^' 11 WindowsComponentProject
Error 5 error C2011: 'HelloWorldC' : 'class' type redefinition 6 WindowsComponentProject
Error 6 error C3699: '*' : cannot use this indirection on type 'HelloWorldC' 18 WindowsComponentProject
Error 7 error C2750: 'HelloWorldC' : cannot use 'new' on the reference type; use 'gcnew' instead 18 WindowsComponentProject
Error 8 error C2440: 'initializing' : cannot convert from 'HelloWorldC *' to 'HelloWorldC ^' 18 WindowsComponentProject
Error 9 error C2027: use of undefined type 'HelloWorldC' 21 WindowsComponentProject
Error 10 error C2227: left of '->callCSharpHelloWorld' must point to class/struct/union/generic type 21 WindowsComponentProject
I just seek some sites for the solution to change the properties of CLR but it doesn't works kindly help me over it.. thanks in advance !!!
You are using old Managed C++ syntax.
New one is called C++/CLI.
You create a reference type by prefixing the class declaration with ref keyword. Use ^ to declare a reference variable as in CSharpHelloWorld^ t.
gcnew is what you use to create an object in managed heap.
You should modify your class as shown below.
using namespace System;
public ref class HelloWorldC {
public:
// Provide .NET interop and garbage collecting to the pointer.
CSharpHelloWorld^ t;
HelloWorldC() {
t = gcnew CSharpHelloWorld();
// Assign the reference a new instance of the object
}
// This inline function is called from the C++ Code
void callCSharpHelloWorld() {
t->displayHelloWorld();
}
};
I am getting the following error when I try to run my program: Exception in thread "main" java.io.FileNotFoundException: Could not locate apply
/clojure/core/vector__init.class or apply/clojure/core/vector.clj on classpath:
, compiling:(erbium/compile.clj:1:1). It seems to be pointing to file below and suggests that I need to put clojure.core/vector in my dependencies. Is it not included by default?
(ns erbium.compile
(require `[clojure.string :as string])
)
(defn produce-out "Convert 'command %1 %2 %3' with stack [5 6 7] to 'command 5 6 7'" [word stack definitions]
(let [
code (definitions word) ; dictionary/hash lookup. eg. "println" -> "echo $1"
replacement (fn [match] (-> match second Long/parseLong dec stack str))
]
; evaluate arguments. eg. "echo %1"
; stack=["blah"]
; -> "echo blah"
(string/replace code #"%(\d)" replacement)
)
)
(defn parse-word "Verifies that word is in defintitions and then returns (produce-out word stack)" [word stack definitions]
(if (some #{word} (keys definitions))
(produce-out word stack)
)
)
(defn compile "Main compile function" [code]
(let [
split-code (string/split code #"\s")
definitions {
"println" "echo %1"
"+" "%1 + %2"
"-" "%1 - %2"
}
stack []
]
(for [word [split-code]]
(if (integer? (read-string word))
(do
(println "Found integer" word)
(def stack (conj stack (read-string word)))
(println "Adding to argument stack:" stack)
)
; else
(do
(parse-word word stack definitions)
(def stack [])
)
)
)
)
)
This file is loaded by the core file via (load "compile") if that makes a difference.
the first error I see is this:
(require `[clojure.string :as string])
It should be like this:
(:require [clojure.string :as string])
in a regular clojure source file. This fixed it for me.
That said, here comes some general advices:
There are a lot of formatting "mistakes". Of course you can format your code as you would like to, however, it's easier for others to follow if you stick to basic formatting principles. Here is a nice collection of them: https://github.com/bbatsov/clojure-style-guide Most editors implement some formatting tool.
split-code (str/split code #"\s") this won't work as you required [clojure.string :as string] So change it to: split-code (string/split code #"\s")
I am not sure about (load ...) and in which context one uses that generally. However, for starting to learn clojure I recommend Lighttable as it has instant feedback built in which is very valuable when learning something new.
To expand on the answer, the "require" appears in the namespace declaration "ns". ns is actually a macro that expands to a series of statements to create the namespace, and do some setup.
This macro treats statements like (:require ...) as calls to a function with the name require, and automatically quotes any following arguments. Since you had specified a quote yourself:
(ns erbium.compile
(require '[clojure.string :as string]))
Then the result ends up being double-quoted, and the call to require ends up being:
... (require (quote (quote [clojure.string :as string])))
So it ends up trying to load a namespace called "quote" followed by a vector that was syntactically in the wrong place. :)
The ns macro is a standard and convenient way to set up namespaces, but it took me a long time to learn it properly. I found the best way was to copy other people's setup code until I learnt how to do it right.
Incidentally, the use of require instead of :require does not matter, though the standard is to use :require so it does not look like a direct call to that function.
If I try to evaluate the following code in my emacs cider-repl, nil is returned, as expected, but none of the printing takes place in the repl buffer or console. How can I make this print out as intended?
(dotimes [i 5]
(.start
(Thread.
(fn []
(Thread/sleep (rand 500))
(println (format "Finished %d on %s" i (Thread/currentThread)))))))
;=> nil
This works fine, however:
(println (format "Finished 1 on %s" (Thread/currentThread)))
;=> Finished 1 on Thread[nREPL-worker-18,5,main]
----------- mini-buffer -----------------
nil
The behavior of println is to use a dynamically bound var called *out* as its output stream. emacs dynamically binds *out* to go to the repl buffer for code evaluated in the repl buffer, but if you create a thread, that thread's *out* gets the root binding of *out*, which in the case of cider will not be the repl buffer.
If you started the repl using cider-jack-in, when you look at you buffer list there should be a buffer with a name like *nrepl-server* which contains the output of the root *out* binding. Here is the contents of mine after running your code:
nREPL server started on port 52034 on host 127.0.0.1 - nrepl://127.0.0.1:52034
Finished 1 on Thread[Thread-9,5,main]
Finished 0 on Thread[Thread-8,5,main]
Finished 2 on Thread[Thread-10,5,main]
Finished 3 on Thread[Thread-11,5,main]
Finished 4 on Thread[Thread-12,5,main]
If you did not use cider-jack-in, the output will print to the terminal where you started the nrepl process.
*out* is the dynamic variable determining where output from println and similar functions goes. It is thread-bound to someplace that causes stuff to be sent back to emacs for display by cider; if you start a new thread, that binding is not present, and the output goes elsewhere (probably to the stdout of the nrepl server emacs/leiningen started in the background).
You can address this in a few ways. You could capture the value of *out* from the parent thread, and then pass it along to the child thread in a closure, and rebind *out* to it:
(let [out *out*]
(.start (Thread. (fn []
(binding [*out* out]
(println "test"))))))
Or you can use a future instead of starting the thread yourself: Clojure automatically conveys relevant thread-local bindings to new threads started for a future.
Execute the following expression in the repl, then all output will end up in the repl:
(alter-var-root #'*out* (constantly *out*))
original answer:
https://groups.google.com/d/msg/cider-emacs/bIVBvRnGO-U/nDszDbGoVzgJ
If you are using Figwheel, then doing prn/println in ring handlers (which are actually similar to the Threads example shown above) can also be swallowed by Fighweel itself. Check your project's project.clj (look for the key :server-logfile inside the :figwheel map), where you can control if out should go to the repl or to a logfile. Please note, this only applies if you are using figwheel, otherwise the printing to the REPL of course works fine.
See my answer on this question for more details: Output compojure server print statements into figwheel terminal?
I'm very new in learning Clojure. This intended to be my first and very simple Clojure tries in which I call a simple Clojure method from inside java code. Unfortunately it does not work. The Compilation is successful and from the Clojure REPL the written function does as it was ordered, but when calling from Java it says the following:
Exception in thread "main" java.lang.IllegalArgumentException: Wrong number of args (2) passed to: ClojNum$-myinc
at clojure.lang.AFn.throwArity(AFn.java:439)
at clojure.lang.AFn.invoke(AFn.java:43)
at com.experimental.clojure.test.ClojNum.myinc(Unknown Source)
at com.experimental.clojure.java.JavaCaller.main(JavaCaller.java:14)
Here is the very simple Clojure code:
(ns com.experimental.clojure.test.ClojNum
(:gen-class
:init init
:name com.experimental.clojure.test.ClojNum
:methods [
[myinc [int] int]
]))
(defn -init [] [[] (atom [])])
(defn myinc "comment" [x] (+ x 1))
(defn -myinc "comment" [x] (myinc x))
And the java part:
package com.experimental.clojure.java;
import com.experimental.clojure.test.ClojNum;
public class JavaCaller {
/**
* #param args
*/
public static void main(String[] args) {
int i = 0;
System.out.println(i);
ClojNum c = new ClojNum();
i = c.myinc(0);
System.out.println(i);
}
}
What did I do wrong?
(Note again: This is primitve test code just to make a first successful function call)
Thanks for the help, I'm clueless.
Jeremy's link in the comments show you one way to call a static method in a clojure class. If you want to call a clojure function on an object instance, you need to add a parameter to your wrapper method definition:
(defn -myinc "comment" [this x] (myinc x))
The 'this' parameter is required for any non-static wrapper function. Clojure threw an exception because it received two parameters for a function only defined with one. Note, you do not change anything in your :gen-class :methods section or the myinc function definition itself.
The documentation is a bit sparse, but examples of this can be found at:
http://clojure.org/compilation (the last example on the page shows instance methods).