closures

Making variables captured by a closure volatile

耗尽温柔 提交于 2019-12-06 18:31:54
问题 How do variables captured by a closure interact with different threads? In the following example code I would want to declare totalEvents as volatile, but C# does not allow this. (Yes I know this is bad code, it's just an example) private void WaitFor10Events() { volatile int totalEvents = 0; // error CS0106: _someEventGenerator.SomeEvent += (s, e) => totalEvents++; while(totalEvents < 10) Thread.Sleep(100); } EDIT : People seem to be missing the point of my question a bit. I know I can't use

Determining, if a variable is a valid closure in PHP

醉酒当歌 提交于 2019-12-06 16:41:04
问题 Using the following function: function is_closure($t) { return ( !is_string($t) && is_callable($t)); } Can this return true for anything else, than an anonymous closure function? If so, what would be the correct way to determine, if a variable is a closure? Many thanks 回答1: The most deterministic way to check if a callback is an actual closure is: function is_closure($t) { return $t instanceof Closure; } All anonymous functions are represented as objects of the type Closure in PHP. (Which,

get the value of variable out of closure swift

妖精的绣舞 提交于 2019-12-06 16:29:46
问题 So I have a closure called task which is a post request and I have a global variable called values and I'm trying to set the value of "values" to be the data that I retrieved back from database stored in a variable called "array". Don't worry about the tableview.reloadData part, that's already done. i just wanna know how to get the value out of closure. var values:NSArray = [] @IBOutlet weak var Open: UIBarButtonItem! override func viewDidLoad() { super.viewDidLoad() Open.target = self

Swift iOS: Closure will not run in background when Remote Notification received

China☆狼群 提交于 2019-12-06 16:06:00
I am writing an iOS app that requires the device's GPS loction to be updated when a push notification is received. I use a closure to get the current GPS location. This code runs perfectly when the app is in the foreground (Both "New remote notification" and "Got location" is printed to console), but when the app is in the background, only "New remote notification" (and no location error) is printed to console. As a result I don't have the GPS location of the device at this point, which is very important for my app's functionality. Any help would be greatly appreciated. Thanks. I have in my

Can I use Javascript Closures here instead of a global variable?

こ雲淡風輕ζ 提交于 2019-12-06 15:51:49
问题 Current setup: var placeId; function selectPlace(place) { $('#selectPlace').html('Selected Place: <b>' + place.Name + '</b>'); $('#map').hide(400); placeId = place.Id; } $(document).ready(function() { $('#postMessage').click(function() { alert("PlaceId: " + placeId); }); }); Can/should I be using closures? 回答1: Based on your comments, it seems like what you're looking for is this: function selectPlace(place) { if(!place){ return selectPlace.placeId; }else{ $('#selectPlace').html('Selected

Why Apple's closure declarations missing argument labels

醉酒当歌 提交于 2019-12-06 15:47:05
As I read through the UIKit , most of the time I see closures (as a function parameter) with missing argument labels like this: func fooClosure(fooClosure: (Bool) -> Swift.Void) In some cases I can guess what it stands for, in others I can't. Example: Take a look at one of UIActivityViewController 's closure (as a type alias): public typealias UIActivityViewControllerCompletionWithItemsHandler = (UIActivityType?, Bool, [Any]?, Error?) -> Swift.Void What does 3rd type stand for? I have to look at the Objective-C version of the code to know these are returnedItems . Why it is not implemented

C++ COM Object Hotpatching?

…衆ロ難τιáo~ 提交于 2019-12-06 15:26:41
问题 Background: I'm hooking on windows COM object. The method used is vtable modification. say we have an instance of interface A named instance , it contains oldmethod in the interface, I replaced with newmethod . However, in my newmethod I need to know the address of oldmethod so that I can call oldmethod after doing my own thing. It is not safe to store the address of oldmethod in a global variable, since there might be more than one implementation behind interface A , let's say there are two

simple closure vs closure with nested function return

不问归期 提交于 2019-12-06 14:52:02
var names = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight"]; var digit_name = function(n){ return names[n]; } //Execute digit_name(0) VERSUS var digit_name = (function() { var names = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight"]; return function(n) { return names[n]; } })(); then execute it like this: digit_name(2) I know these are both closures, but I also think that there are some fundamental differences between the way the two are setup. Can someone point out how different are these two setups (especially considering both get the same job

Groovy DSL: creating dynamic closures from Strings

五迷三道 提交于 2019-12-06 14:39:22
There are some other questions on here that are similar but sufficiently different that I need to pose this as a fresh question: I have created an empty class, lets call it Test. It doesn't have any properties or methods. I then iterate through a map of key/value pairs, dynamically creating properties named for the key and containing the value... like so: def langMap = [:] langMap.put("Zero",0) langMap.put("One",1) langMap.put("Two",2) langMap.put("Three",3) langMap.put("Four",4) langMap.put("Five",5) langMap.put("Six",6) langMap.put("Seven",7) langMap.put("Eight",8) langMap.put("Nine",9)

How to create new closure cell objects?

巧了我就是萌 提交于 2019-12-06 14:06:30
I need to monkey-patch my library to replace an instance of a symbol, and it's getting referenced by some function closures. I need to copy those functions (since I also need access to original unpatched version of the function as well), but __closure__ is immutable, and I can't copy.copy it, so how can I create new closure cells objects in Python 2.7? I for example given this function def f(): def incorrectfunction(): return 0 def g(): return incorrectfunction() return g def correctfunction(): return 42 func = f() patched_func = patchit(f) # replace "incorrectfunction" print func(), patched