在clojure中实例化对象

时间:2013-01-01 04:09:42

标签: clojure

我对clojure非常新,所以我想用java编写代码。

我现有的代码是什么。但它似乎抛出一个CompilerException java.lang.RuntimeException:无法获取宏的值:#'clojure.core / - >,编译:(NO_SOURCE_PATH:2)

此外,它似乎只实例化了一个对象

(defrecord Learning [Name Age Gender])
  (def person 
    (apply -> Learning
           (clojure.string/split
             "Jon,12,Male", #",")
           )
    )

但是,我想要做的是在java中编写代码。 例如,在Java中,我们将有类似

的类
Public class Person {
Private int age;
Private String Gender;
Private String Name;

Person(String Name, String gender, int age)
{
this.name = Name;
this.age = age;
this.Gender = gender;
}
}

To Instantiate an instance of this class we would have a line like 

Person Jon = new Person(Jon, Male, 12); 

我如何在clojure中做到这一点?

我有这个

(defn update [x]
  (def person 
    (apply ->Learning
           (clojure.string/split
             x, #",")
           )
    )
)

但它表示这是一个畸形的表达。我想要做的就像在java中创建一个构造函数。

1 个答案:

答案 0 :(得分:2)

为避免编译错误,只需删除->Lerning

之间的空格即可
(def person (apply ->Learning
                   (clojure.string/split
                    "Jon,12,Male", #",")))

它会起作用

person
-> Learning{:Name "Jon", :Age "12", :Gender "Male"}