在if-else中执行多个语句而无nullpointer异常 [英] Executing multiple statements in if-else without nullpointer exception

查看:113
本文介绍了在if-else中执行多个语句而无nullpointer异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更深入地研究Clojure和函数式编程。



在我的代码中,我有一个(def服务器(spawn-server))。现在,我需要一个简短的函数供REPL检查此套接字的状态。



此刻我所拥有的是:

 (定义状态[] 
(如果服务器

(println服务器已启动并正在运行))
(println连接:(连接计数服务器))

(println服务器关闭)))

如果服务器为零,则一切正常,但这是服务器正在运行时REPL上的输出:

  => (状态)
服务器已启动并正在运行
连接:0
#< CompilerException java.lang.NullPointerException(NO_SOURCE_FILE:0)>

我不太确定是否看到问题,但是我不知道如何解决应该工作:-)
我在这里是这样的:

 ((println foo)(println foo))

,其计算结果为(nil nil)会导致NullPointerException吗?



通常我不会使用外括号,但是如何为if创建某种 block语句-健康)状况。如果我不使用它们,第二个println将会被其他使用。



起作用的是将let用作某种阻止语句:

 (让[] 
(println服务器已启动并正在运行),
(println connections:(连接计数服务器)))

但我不确定这是否正确的解决方案?

解决方案

使用

 (defn status [] 
(如果服务器
(执行
(println服务器已启动并正在运行)
(println连接:(连接计数服务器)))
(println服务器已关闭))))

在Lisps中,通常不能只添加括号以进行分组。

 ((println foo)(println foo))

此处,返回值将尝试调用第一个(println foo)的函数(作为函数),并返回第二个论点。这些是非常基本的评估规则,因此,我建议您阅读一些有关Clojure或Lisps的入门书籍或文档。



Clojure主页:评估部分>


非空列表被视为调用
到特殊形式,宏或
函数。呼叫的格式为
(运算符*)。


宏或特殊格式可能会破坏该规则。 / p>

I'm trying to dig a little deeper into clojure and functional programming.

At some point of my code I have a (def server (spawn-server)). Now I want a short function for the REPL to check the state of this socket.

This is what I have at the moment:

(defn status []
  (if server 
    (
      (println "server is up and running")
      (println "connections:" (connection-count server)) 
     )    
    (println "server is down")))

If server is nil, everything works fine, but this is the output on the REPL if the server is running:

=> (status)
server is up and running
connections: 0
#<CompilerException java.lang.NullPointerException (NO_SOURCE_FILE:0)>

I'm not really sure if I see the problem, but I can't figure out how this should work :-) What I have here is this:

((println "foo")(println "foo"))

which will be evaluated to (nil nil) which results in the NullPointerException ?

Usally I wouldn't use the outer parentheses but how can I create some kind of "block" statement for the if-condition. If I don't use them, the second println will be used as else.

What would work is the usage of let as some kind of "block"-statement:

(let [] 
  (println "server is up and running"),
  (println "connections:" (connection-count server)) )

But I'm not really sure if this the "right" solution?

解决方案

Use do:

(defn status []
  (if server 
    (do
      (println "server is up and running")
      (println "connections:" (connection-count server)))    
    (println "server is down")))

In Lisps, generally, you can't just add parens for grouping.

((println "foo") (println "foo"))

Here, the return value of the first (println "foo") will be tried to be called (as a function), with the return value of the second as an argument. Those are very basic evaluation rules, so I suggest that you hit some introductory books or documentation about Clojure or Lisps in general.

From the evaluation section of the Clojure homepage:

Non-empty Lists are considered calls to either special forms, macros, or functions. A call has the form (operator operands*).

Macros or special forms may "break" this rule.

这篇关于在if-else中执行多个语句而无nullpointer异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆