closures

The scope of variable in inner anonymous method

a 夏天 提交于 2020-01-04 13:47:56
问题 See the code public class Test { public static void Main() { Test t = new Test(); var AnonymousMethod = t.OuterMethod(); AnonymousMethod("passedValue"); } public delegate void SampleDelegate(string InputText); public SampleDelegate OuterMethod() { string outerValue="outerValue"; return (x => { Console.WriteLine(x); Console.WriteLine(outerValue); }); } } OutPut Success time: 0.02 memory: 33816 signal:0 passedValue outerValue Link to Example Normally the scope of variable outerValue would end

How do I access/modify variables from a function's closure?

为君一笑 提交于 2020-01-04 07:37:07
问题 If I have a function which has some non-local variable (in a closure), how do I access that variable? Can I modify it, and if so, how? Here's an example of such a function: def outer(): x = 1 def inner(y): nonlocal x return x + y return inner inner = outer() # how do I get / change the value of x inside inner? (apologies if this is already answered elsewhere; I couldn't find it, so I thought I would share the answer once I worked it out) 回答1: A function's enclosed variables are stored as a

Passing a closure that modifies its environment to a function in Rust

≯℡__Kan透↙ 提交于 2020-01-04 07:36:41
问题 I have a closure that captures and modifies its environment. I want to pass this closure to a function that accepts closures: fn main() { let mut integer = 5; let mut closure_variable = || -> i32 { integer += 1; integer }; execute_closure(&mut closure_variable); } fn execute_closure(closure_argument: &mut Fn() -> i32) { let result = closure_argument(); println!("Result of closure: {}", result); } Because the closure modifies its environment, this fails: error[E0525]: expected a closure that

Swift accessing class variable inside closures

隐身守侯 提交于 2020-01-04 06:08:34
问题 Why in Objective-C I can set instance variable inside a block: @interface CMVServices : UIViewController @property (nonatomic, strong) NSMutableArray *times; @implementation CMVServices @synthesize times=_times; and set the _times instance variable inside a block: (some code ) . . [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { [_times addObjectsFromArray:objects]; } but I can't in Swift? class ViewController: UIViewController var times :AnyObject[]! query

Swift accessing class variable inside closures

蹲街弑〆低调 提交于 2020-01-04 06:06:54
问题 Why in Objective-C I can set instance variable inside a block: @interface CMVServices : UIViewController @property (nonatomic, strong) NSMutableArray *times; @implementation CMVServices @synthesize times=_times; and set the _times instance variable inside a block: (some code ) . . [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { [_times addObjectsFromArray:objects]; } but I can't in Swift? class ViewController: UIViewController var times :AnyObject[]! query

Chaining multiple async functions in Swift

允我心安 提交于 2020-01-04 05:51:53
问题 I'm trying to write a series of functions that will validate the user's information before asking them to confirm something. (Imagine a shopping app). I first have to check that the user has added a card. Then I have to check that they have sufficient balance. Then I can ask them to confirm the payment. I can write the async method to check the card something like ... func checkHasCard(completion: (Bool) -> ()) { // go to the inter webs // get the card // process data let hasCard: Bool = //

Better “nothing function” in Swift?

末鹿安然 提交于 2020-01-04 05:26:22
问题 I have this structure, typealias Tweak = ( grab:(_ p: SomeClass)->CGFloat, change:(_ p: SomeClass, _ n: CGFloat)->(), from:CGFloat, upto:CGFloat ) (So, the first line "gives you a value", the second "changes something", the last two are just limits.) So, you might have an array of such things ... var tweaks:[Tweak] = [ ({_ in 0}, {_ in}, 0,0), ( {p in mainHeight}, {p, n in mainHeight = n paintDisplayWhatever()}, 8.0, 28.0), ( {p in Some.global}, {p, n in Some.global = n fireball.adjust(heat:

JavaScript/jQuery dropdownlist change event with closure not working

帅比萌擦擦* 提交于 2020-01-04 04:14:24
问题 In my sample code, I have 2 dropdownlists (but in the real code the number varies because the dropdownlists are created dynamically) and what I wanted to do is to count how many dropdownlists are selected with a non-zero value . I used closure to keep track of the total number of dropdownlists with non-zero selected value. I was successful in doing that (refer to http://jsfiddle.net/annelagang/scxNp/9/). Old/Working Code $("select[id*='ComboBox']").each( function() { $(this).change( function(

JavaScript/jQuery dropdownlist change event with closure not working

感情迁移 提交于 2020-01-04 04:14:22
问题 In my sample code, I have 2 dropdownlists (but in the real code the number varies because the dropdownlists are created dynamically) and what I wanted to do is to count how many dropdownlists are selected with a non-zero value . I used closure to keep track of the total number of dropdownlists with non-zero selected value. I was successful in doing that (refer to http://jsfiddle.net/annelagang/scxNp/9/). Old/Working Code $("select[id*='ComboBox']").each( function() { $(this).change( function(

The #selector is not compatible with the closure?

不打扰是莪最后的温柔 提交于 2020-01-04 03:01:06
问题 I have tried to implement the custom function with the closure. But it's does not supported by #selector . Here's an example: class Core: NSObject { static let shared:Core = Core.init() func button(viewController: UIViewController, button: UIButton, title: String, color: UIColor, completion: () -> Void) { button.layer.cornerRadius = button.bounds.width / 2 button.setTitle(title, for: .normal) button.setTitleColor(UIColor.white, for: .normal) button.backgroundColor = color button.titleLabel?