首页 > 解决方案 > Why does my clojure REPL output gibberish?

问题描述

I'm prototyping some really basic functionality, but the REPL is outputting totally random stuff.

Just trying to write a function to update a value in a map gives this:

fwd.core=> (fn [step] (update {:x 10 :y 10} :x 20) 20)
#object[fwd.core$eval1593$fn__1594 0x3d2da438 "fwd.core$eval1593$fn__1594@3d2da438"]

标签: clojureread-eval-print-loop

解决方案


函数是 Clojure 中的一等公民。因此,您定义了一个匿名函数(请注意,这里接近无操作,因为您不坚持它-而且您在错误的地方也有一些括号(见下文))和 RE P我给你打印了。

所以这基本上是.toString()JVM 看到的函数对象的表示。

边注:

为了更好地命名您的堆栈跟踪,您还可以命名 anon fns,例如:

user=> (fn [step] (update {:x 10 :y 10} :x 20) 20)
#<Fn@559d19c user/eval8096[fn]>
user=> (fn my-fancy-name [step] (update {:x 10 :y 10} :x 20) 20)
#<Fn@451816fd user/eval8105[my_fancy_name]>

编辑(该功能存在多个问题)

至于函数本身,内部更新也是无操作的,因为您也没有分配更新的结果。该函数现在总是返回20

要调用该函数,您必须修复括号:

user=> ((fn [step] (update {:x 10 :y 10} :x step)) 20)
Execution error (ClassCastException) at user/eval8105$fn (REPL:1).
java.lang.Long cannot be cast to clojure.lang.IFn

(该函数首先用于周围的括号,因此将调用它。

这现在给出了一个错误,因为update需要一个函数 -assoc 改为使用:

user=> ((fn [step] (assoc {:x 10 :y 10} :x step)) 20)
{:x 20, :y 10}

推荐阅读