Calling Clojure from .NET

后端 未结 2 550
無奈伤痛
無奈伤痛 2021-01-31 10:39

I have been spending some time playing with Clojure-CLR. My REPL is working, I can call .NET classes from Clojure, but I have not been able to figure out calling compiled Clojur

2条回答
  •  天涯浪人
    2021-01-31 11:14

    I was able to get it to work doing the following:

    First I changed your code a bit, I was having trouble with the namespace and the compiler thinking the dots were directories. So I ended up with this.

    (ns hello
      (:require [clojure.core])
      (:gen-class
       :methods [#^{:static true} [output [int int] int]]))
    
    (defn output [a b]
      (+ a b))
    
    (defn -output [a b]
      (output a b))
    
    (defn -main []
      (println (str "(+ 5 10): " (output 5 10))))
    

    Next I compiled it by calling:

    Clojure.Compile.exe hello

    This creates several files: hello.clj.dll, hello.clj.pdb, hello.exe, and hello.pdb You can execute hello.exe and it should run the -main function.

    Next I created a simple C# console application. I then added the following references: Clojure.dll, hello.clj.dll, and hello.exe

    Here is the code of the console app:

    using System;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                hello h = new hello();
                System.Console.WriteLine(h.output(5, 9));
                System.Console.ReadLine();
            }
        }
    }
    

    As you can see, you should be able to create and use the hello class, it resides in the hello.exe assembly. I am not why the function "output" is not static, I assume it's a bug in the CLR compiler. I also had to use the 1.2.0 version of ClojureCLR as the latest was throwing assembly not found exceptions.

    In order to execute the application, make sure to set the clojure.load.path environment variable to where your Clojure binaries reside.

    Hope this helps.

提交回复
热议问题