closures

View a PHP Closure's Source

…衆ロ難τιáo~ 提交于 2019-12-05 01:02:23
Is it possible to reflect into or otherwise view the source of a PHP closure object? That is, if I do something like this $closure = function() { return 'Hi There'; }; and then something like this var_dump($closure); PHP outputs object(Closure)[14] That is, I know the object's a closure, but I have no idea what it does. I'm looking for a reflection method, function, or debugging extension that will allow me to dump the actual body of anonymous function. What you can get from PHP is limited, using reflection you can just obtain the parameter signature of the function and the start and ending

Swift closure crashes when called as Objective-C block

大憨熊 提交于 2019-12-05 00:26:43
In my project, I have both Objective-C and Swift code. I have some objects that have properties containing blocks to clean up some UITableView configuration. Using it works in Objective-C, but crashes when using Swift. I have reduced the problem to be as minimal as possible, while still being reproducible. // in Objective-C @interface MyClass : NSObject @property (copy, nonatomic) NSString* (^block)(); - (NSString *)callTheBlock; @end @implementation MyClass - (NSString *)callTheBlock { if (self.block) { return self.block(); } else { return @"There is no spoon"; } } @end // In Swift // I

Reusing part of Grails criteria closure

半腔热情 提交于 2019-12-05 00:24:36
I have a fairly large criteria closure in my Grails application, and I would like to reuse part of it in several places in my application. Rather than duplicating the section I need to reuse, I'd like to define this as a separate closure and reference it wherever it is needed, but I am struggling a bit with the syntax. This is a simplified / cut down version, but essentially my criteria looks something like this: def criteriaClosure = { and { // filtering criteria that I'd like to reuse in lots of places or { names.each { name -> sqlRestriction(getFilteringSql(name), [someId]) } } if

Wait for completion handler to finish - Swift

你。 提交于 2019-12-05 00:18:59
问题 I am trying to check if UserNotifications are enabled and if not I want to throw an alert. So I have a function checkAvailability which checks multiple things, including the UserNotification authorization status. func checkAvailabilty() -> Bool { // // other checking // var isNotificationsEnabled = false UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound], completionHandler: { (granted, error) in if granted { isNotificationsEnabled = true } else {

Closure/scope JavaScript/jQuery

北城以北 提交于 2019-12-05 00:05:28
问题 I'm trying to group some exisiting top-level functions inside a closure (to avoid polluting the global namespace) but I'm not quite getting it to work. First, all the JS works outside my anonymous function, but once I put it in the anonymous function I get an error of "crossfade is not defined". Does anyone see anything completely obvious that I am missing? I'm not quite getting why the the setInterval/crossfade works outside the anonymous function but not inside. Anything inside start()

Automapper + EF4 + ASP.NET MVC - getting 'context disposed' error (I know why, but how to fix it?)

佐手、 提交于 2019-12-04 23:14:39
问题 I have this really basic code in a MVC controller action. It maps an Operation model class to a very basic OperationVM view-model class . public class OperationVM: Operation { public CategoryVM CategoryVM { get; set; } } I need to load the complete list of categories in order to create a CategoryVM instance. Here's how I (try to) create a List<OperationVM> to show in the view. public class OperationsController : Controller { private SomeContext context = new SomeContext (); public ViewResult

Decorator and closures

╄→尐↘猪︶ㄣ 提交于 2019-12-04 21:50:11
I am going through the How to make a chain of function decorators? to understand decorator. In the following example, we see that "method_to_decorate" is accessible to wrapper function because of closures. But, I didn't understand how arguments self and lie are accessible to the wrapper function. def method_friendly_decorator(method_to_decorate): def wrapper(self, lie): lie = lie - 3 # very friendly, decrease age even more :-) return method_to_decorate(self, lie) return wrapper class Lucy(object): def __init__(self): self.age = 32 @method_friendly_decorator def sayYourAge(self, lie): print "I

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

青春壹個敷衍的年華 提交于 2019-12-04 21:48:24
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? 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 Place: <b>' + place.Name + '</b>'); $('#map').hide(400); selectPlace.placeId = place.Id; } } $(document).ready

difference between closures and continuations

北城余情 提交于 2019-12-04 21:39:19
问题 Can someone please explain the difference between closures and continuations? The corresponding articles in wikipedia do not really compare the differences between the two. 回答1: A closure is a function that captures data from the environment on which it was declared. int myVar = 0; auto foo = [&] () { myVar++; }; <- This lambda forms a closure by capturing myVar foo(); assert(myVar == 1); A continuation is a more abstract concept, and refers to what code should be executed afterwards. It can

PowerShell: an elegant way to create closures

百般思念 提交于 2019-12-04 21:22:07
问题 Keith Hill explained me that blocks in PowerShell are not closures and that to create closures from blocks I have to call method .GetNewClosure(). Is there any elegant way to create closures from blocks? (e.g. create a wrapping function, an alias?, ...) Example: { block } ${ closure } # ??? 回答1: You could create a function that takes a scriptblock, calls GetNewClosure and returns the closure. It is essential that you call this function using the dot operator e.g.: function =>([scriptblock]$