首页 \ 问答 \ 是否有一个可以重载的groovy逻辑非运算符(如unaryNot ..)(is there a groovy logical not operator (like unaryNot..) that can be overloaded)

是否有一个可以重载的groovy逻辑非运算符(如unaryNot ..)(is there a groovy logical not operator (like unaryNot..) that can be overloaded)

我在groovy中有一个Rule类,我可以重载<<,和,或者对于类实例,但如果我想写

Rule a = new Rule()
a<< {false }  //overloaded evaluate internal closure 

println !a // expect to get true - but you always get false

我无法找到groovy逻辑'not'运算符是什么。 如果我打电话给'!a'我总是假的,因为我认为编译器正在使用'a'的常设真理是'真'所以'!''将是假的。

但是我想要'!a'来表示这个类的'!(a.evaluate())'

我怎么做 ?

PS - 扩展 - 这里是我的规则类的虚拟模拟视图 - 删除了所有修剪 - 这可以在脚本中运行

class Rule {
    Closure  evaluate
    def leftShift (closure ) {
        this.evaluate = closure
    }


}

def r = new Rule ()
r << {false}

println "logical not of rule : " + !r
println " rule evaluate  : " + !r.evaluate ()

r << {true}
println "logical not of rule : " + !r
println " rule evaluate  : " + !r.evaluate ()

I have a Rule class in groovy and i can overload <<, and, or for class instance but if i want to write

Rule a = new Rule()
a<< {false }  //overloaded evaluate internal closure 

println !a // expect to get true - but you always get false

i cant find what the groovy logical 'not' operator is. If i call '!a' i always get false as i think that compiler is using groovy truth of 'a' is 'true' so '!a' would be false.

but i want the '!a' to mean '!(a.evaluate())' for this class

how do i do that ?

PS - extended - here is dummy mocked up view of my Rule class - all trimmings removed - this is runnable in a script

class Rule {
    Closure  evaluate
    def leftShift (closure ) {
        this.evaluate = closure
    }


}

def r = new Rule ()
r << {false}

println "logical not of rule : " + !r
println " rule evaluate  : " + !r.evaluate ()

r << {true}
println "logical not of rule : " + !r
println " rule evaluate  : " + !r.evaluate ()

原文:https://stackoverflow.com/questions/35719869
更新时间:2023-02-25 15:02

最满意答案

您可以在global对象中声明变量io ,以便从任何位置都可以看到它。

global.io = socketIO.listen(server)


You can declare the variable io inside the global object so that it is visible from everywhere.

global.io = socketIO.listen(server)

相关问答

更多
  • 尝试使用require('socket.io')将socket.io模块添加到您的文件中 var server = require('http').createServer(); var io = require('socket.io')(server); io.on('connection', function(client){ client.on('event', function(data){}); client.on('disconnect', function(){}); }); serv ...
  • 回答: 1)不,你不需要指定IP,你甚至可以使用/它将通过默认的HTTP 80端口,无论如何,你可以在socket.io网站上找到更多的例子。 2)也需要io ,记得在你的包中添加socket.io-client : var React = require('react'), io = require('socket.io-client'); 无论如何,如果你想包含socket.io服务器提供的客户端脚本作为静态文件,那么记得使用