closures

How do Rust closures work and how does it execute a closure?

无人久伴 提交于 2019-12-10 18:16:20
问题 Does it create a new thread and then execute that anonymous function inside the new thread? I noticed many ownership / borrowing restrictions when I'm working with a closure. For example, if I have Fn() , I cannot pass a mutable variable inside the closure or it needs to be wrapped with a Mutex: fn helloworld(f: &Fn(f64)) { f(42f64); } pub fn main() { let mut killer = 2; helloworld(&|n| { println!("{}", n); killer += 1; }); } If a closure can be unsafe like that then something asynchronous or

How to dynamically create a Groovy Closure from a String in Java

送分小仙女□ 提交于 2019-12-10 18:14:30
问题 I would like to know how to create a Closure object at run-time from within a Java application, where the content of the Closure is not known ahead of time. I have found a solution but I doubt that it is optimal. Background: I have written some Groovy code that parses a Domain Specific Language. The parsing code is statically compiled and included in a Java application. In the parser implementation I have classes acting as delegates for specific sections of the DSL. These classes are invoked

What does “borrowed data cannot be stored outside of its closure” mean?

﹥>﹥吖頭↗ 提交于 2019-12-10 17:39:04
问题 When compiling the following code: fn main() { let mut fields = Vec::new(); let pusher = &mut |a: &str| { fields.push(a); }; } The compiler gives me the following error: error: borrowed data cannot be stored outside of its closure --> src/main.rs:4:21 | 3 | let pusher = &mut |a: &str| { | ------ --------- ...because it cannot outlive this closure | | | borrowed data cannot be stored into here... 4 | fields.push(a); | ^ cannot be stored outside of its closure What does this error mean, and how

can ajax callback function see variables from parent function?

▼魔方 西西 提交于 2019-12-10 17:37:37
问题 function validateHistoryId(input) { $.getJSON('request.php', {"action": "historyExists", "history_id" : input.value}, function(data) { console.log(input.value); } ); } i use jQuery ajax call from javascript function. I tried the above code and it works, but I don't know will it cause any problems in the future. I want to know, can ajax callback function see variables of it's parent function? and is it a bad practice to do so? 回答1: This is the JavaScript functional way to doing things. It's

How does the JS scope of these blocks work?

徘徊边缘 提交于 2019-12-10 17:23:03
问题 Can anyone explain why the following produces 1,2 and the other produces 5? Should they not both produce 5? //produces 1,2 (function () { var a = [5]; function bar() { if (!a) { var a = [1, 2]; } console.log(a.join()); } bar(); })(); Based on reading some articles about JS closure, I expect them both to produce 5. Can't seem to find an article anywhere that would give some insight as to why the first block produces otherwise. //produces 5 (function () { var a = [5]; function bar() { if (a) {

XCTAssertThrowsError strange behavior with custom errorHandler

一笑奈何 提交于 2019-12-10 16:56:17
问题 In my unit test I have the following code that checks whether thrown error is of expected type. It is done with two identical statements one of which doesn't compile: enum Error: ErrorType { case SomeExpectedError case SomeUnexpectedError } func functionThatThrows() throws { throw Error.SomeExpectedError } // 1) this compiles fine XCTAssertThrowsError(try functionThatThrows()) { (error) in switch error { case Error.SomeExpectedError: break //everything is fine case Error.SomeUnexpectedError:

Can non-anonymous functions in PHP using 'use' keyword?

微笑、不失礼 提交于 2019-12-10 16:36:22
问题 Can non-anonymous functions in PHP using 'use' keyword? Or it is available for anonymous functions only. Can I write a php file like this // L.php // assume $_texts is in this context.. $_language = null; function L_init($language) use (&$_language) { $_language = $language; } function L($key) use ($_texts, $_language) { $_texts[$_language][$key]; } So, another file can use it like this // client.php require_once 'L.php'; L_init('en'); echo L('GREETING'); // Will output localize string of key

Is there any way to explicitly write the type of a closure?

感情迁移 提交于 2019-12-10 15:49:52
问题 I started reading the Rust guide on closures. From the guide: That is because in Rust each closure has its own unique type. So, not only do closures with different signatures have different types, but different closures with the same signature have different types, as well. Is there a way to explicitly write the type signature of a closure? Is there any compiler flag that expands the type of inferred closure? 回答1: No. The real type of a closure is only known to the compiler, and it's not

Best way to test Javascript functions that use variables from a closure?

末鹿安然 提交于 2019-12-10 15:49:48
问题 So the problem I'm having is I have a function which uses a variable in a closure and when testing it it returns a reference to the variable in it's scope. My code looks similar to the following: var app = function() { var theString = ""; //Appends ztvars onto the url passed into the function var appendString = function(url) { if (theString.length === 0) { return url; } return url+theString; }; //Used for testing, returns the function appendPageVars this.returnFunctions = function() { return

Groovy: Execute Code transparent before and after any method is invoked

故事扮演 提交于 2019-12-10 15:39:17
问题 Let's say we have a groovy class with some methods (static or not static). What i want to do is executing some code before and after every method of this class is invoked without touchung the class at all and without dynamically manipulating the code inside of each method. What i tried using groovy metaClass; getting all methods of the metaClass and then dynamically replacing the every method with a wrapping method, containing some code and in the middle invoking the old method. Problem is, i