是否有一个可以重载的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
最满意答案
您可以在
global
对象中声明变量io
,以便从任何位置都可以看到它。
global.io = socketIO.listen(server)
You can declare the variable
io
inside theglobal
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 ...
-
React和Socket.io(React and Socket.io)[2023-08-22]
回答: 1)不,你不需要指定IP,你甚至可以使用/它将通过默认的HTTP 80端口,无论如何,你可以在socket.io网站上找到更多的例子。 2)也需要io ,记得在你的包中添加socket.io-client : var React = require('react'), io = require('socket.io-client'); 无论如何,如果你想包含socket.io服务器提供的客户端脚本作为静态文件,那么记得使用标签将它添加到HTML中,这样你就可以在全局 ... -
Socket.io - listener必须是socket.io事件的函数(Socket.io - listener must be a function on socket.io event)[2021-11-11]
我认为错误是你没有传递函数,而是执行它。 尝试删除括号: socket.on('joinChat', myFuncExportedByModule); I think the error is you're not passing a function, but executing it. Try remove parenthesis: socket.on('joinChat', myFuncExportedByModule); -
节点JS Socket.io(Node JS Socket.io)[2022-02-26]
您可能没有将socket.io客户端库公开给公众。 如果您使用快速生成器,只需将该文件复制并粘贴到您的yourproject/public ,并将您的请求发送到localhost:3000/如果你没有使用express生成器,请创建一个名为public的文件夹,然后在你的主项目文件中放置app.use(express.static(__dirname + '/public')); 然后做我上面描述的 哦,你的错误指的是视图引擎无法 ... -
你的listen函数只是返回另一个函数而不是实际触发代码来设置socketio来监听。 尝试: module.exports.listen = function(app) { io = socketio.listen(app); io.sockets.on('connection', function (socket) { socket.on('disconnect', function () { if (socket.uid !== undefi ...
-
您将需要使用npm install (从命令提示符)来确保在使用之前安装了socket.io包。 npm install --save socket.io --save标志会将给定的依赖项添加到应用程序的package.json ,以便将来更容易安装 You'll want to use npm install (from your command prompt) to ensure the socket.io package is installed before use. npm install -- ...
-
Node.js:哪个模块“require('..')”使用socket.io?(Node.js: Which module to “require('..')” to use socket.io?)[2022-07-10]
它需要父目录中的模块 - 在本例中为socket.io It requires the module from the parent directory - in this case, socket.io -
您可以在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)
-
Socket.io NAMESPACES userType1.js: module.exports = function (io) { var nsp = io.of('/userType1'); nsp.on("connection", function (socket) { console.log("Connected to userType1"); }); } userType2.js module.exports = function (io) { ...
-
JavaScriptCore本身只实现ECMAScript规范 。 像XMLHTTPRequest,Web套接字,DOM等的东西来自WebKit。 如果您绝对想要运行Socket.IO JavaScript客户端,您可以使用隐藏的UIWebView,尽管我建议使用Socket.IO-objc 。 在一个不相关的注释中,您可以通过检查其异常属性或通过设置将错误打印到exceptionHandler属性的块来查看JSContext生成的错误: context.exceptionHandler = ^(JSCon ...