带有休息参数的匿名函数抛出NullPointerException

编程入门 行业动态 更新时间:2024-10-10 08:25:57
本文介绍了带有休息参数的匿名函数抛出NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是函数

(#( (println (str "first: " %1)) (println (str "second: " %2)) (println (str "rest: " (clojure.string/join ", " %&)))) "f" "s" "x" "y" "z")

在苹果酒中运行时我得到了预期的结果,但是最后我看到我也得到了NullPointerException。

When running this in cider I get the desired result but at the end I see that I am also getting a NullPointerException.

这种形式的匿名函数似乎与

It seems that this form of anonymous function has some issues with destructuring.

因为,当我尝试以下形式的匿名函数时,它起作用。

Because, when I try the following form of anonymous function, it works.

((fn [f s & rest] (println (str "first: " f)) (println (str "second: " s)) (println (str (clojure.string/join ", " rest)))) "f" "s" "x" "y" "z")

有人可以解释为什么会这样吗?

Can someone explain why is this happening ?

推荐答案

您需要做:

(#(do (println (str "first: " %1)) (println (str "second: " %2)) (println (str "rest: " (clojure.string/join ", " %&)))) "f" "s" "x" "y" "z")

没有 do ,您正在尝试调用第一个 println 的结果,(即在列表的其余元素上 nil )。 fn 具有隐式 do 。

Without the do, you're trying to invoke the result of the first println, (i.e. nil) on the remaining elements of the list. fn has an implicit do.

情况下,比较((println))和(do(println))

更多推荐

带有休息参数的匿名函数抛出NullPointerException

本文发布于:2023-11-28 17:03:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643242.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:抛出   函数   参数   NullPointerException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!