closures

Closure defined in root not visible in child

半城伤御伤魂 提交于 2019-12-10 22:45:22
问题 I have root project and subproject ( :child ). Root build looks like like this: def foo = { println("foo") } allprojects { task bar << { println(project.name + ":bar") } afterEvaluate { foo() } } Running gradle bar prints: foo foo :bar :child:bar child:bar parent:bar This make sense. However, IRL I need foo to be called by the child's build file (because I want it to be called only by some of the submodules). The documentation seems to be clear enough: In a multi-project build, sub-projects

JavaScript: automatic getters and setters in closures without eval?

烂漫一生 提交于 2019-12-10 22:19:10
问题 Note : there are a lot of questions regarding getters and setters in JavaScript (see Javascript Getters And Setters, Getters and Setters in a function (javascript), Getters and Setters in JavaScript (Correct syntax ?), automatic getter and setter(with validation) in javascript, etc). Most of these questions deal with Objects , a few with Closures and rather few of them with automatically producing getters and sets. This question deals with the later. The answer to automatic getter and setter

Invoking anonymous closure

余生长醉 提交于 2019-12-10 21:48:32
问题 Edit ok, great feedback here, got me pointed in the right direction. Use case for invoking anonymous closure is in Scalatra routing layer. I have a bunch of routes that are grouped together under various types, in this example, requests common to teams: class Router { type TeamType <: _Controller with _Team get("""(schedules|rosters|teamresults|teamstats)/\d{8}""".r) { val clazz :: date = captures val obj = (clazz match { case "schedules" => new RosterController case "rosters" => new

JavaScript onKeyUp closures with timeout

痞子三分冷 提交于 2019-12-10 20:27:52
问题 Im trying to assign an onKeyUp event to all inputs within a form, using closures. the array fields contains all the names of the fields that require the event assigned to them. The array ajaxFields contains the names of the fields (from the array fields ) that requires ajax validation. function createEvents(fields,ajaxFields) { for(var x=0;x<fields.length;x++) { $('input[name='+fields[x]+']').keyup(function(field) { //assign an onKeyUp event return function() { //some code using variable

Closure memory leaks

假装没事ソ 提交于 2019-12-10 20:14:12
问题 I'm interested in the possibility in memory(unneeded reference) leaks of memory leaks in garbage collected languages caused by variables caught in closures which are stored (perhaps as part of an object system or as part of building actions based on input to be evaluated later). Are there any languages where this sort of thing is somewhat common? If so what are the patterns to watch out for in those languages to prevent it? 回答1: As long as the closure is referenced, the captured variables

C# closure heap allocation happening at start of method

蹲街弑〆低调 提交于 2019-12-10 19:11:53
问题 I seem to have run into some odd behavior of the C# compiler. Consider the following code sample: static void Main(string[] args) { Foo(false, 8); } public static void Foo(bool execute, int x) { if (execute) { Task.Run(() => Console.WriteLine(x)); } } Running this (in release) shows some unexpected allocations happening. Examining the IL shows that that the heap allocation triggered by the closure appears at the very start of the function, rather than inside the condition: .method public

python closure + oop

谁都会走 提交于 2019-12-10 19:11:33
问题 I'm trying to do something a bit strange (at least to me) with python closure. Say I have 2 classes like this: #!/usr/bin/python import types def method_a(self): print "ma %d" % self.val class A(object): def __init__(self): self.val = 5 pass def foo(self, a): def closure(self): print "closure %d, %d" % (self.val, a) return closure class B(object): def __init__(self): self.val = 10 pass def foo(self): print "B::foo %d" % self.val a = A() b = B() b.undo = types.MethodType(a.foo(1), b) b.undo()

Making a PHP closure function safe for PHP 5.2

蓝咒 提交于 2019-12-10 19:08:31
问题 The following function works in PHP > 5.3 but errors out in older versions. How can I modify this to make it 5.2 safe? function _iniloader_get_dirs($dir) { $dirs = array_filter(scandir($dir), function ($item) use ($dir) { return (is_dir($dir.'/'.$item) && $item != "." && $item != ".."); }); // Use array_values to reset the array keys: return array_values($dirs); } 5.2 error: Parse error: syntax error, unexpected T_FUNCTION ... on line 2 回答1: You can easily do it w/o the closure, but you will

why does dart create closures when referencing a method?

帅比萌擦擦* 提交于 2019-12-10 19:02:32
问题 void main() { A one = new A(1); A two = new A(2); var fnRef = one.getMyId; //A closure created here var anotherFnRef = two.getMyId; //Another closure created here } class A{ int _id; A(this._id); int getMyId(){ return _id; } } According to the dart language tour page referencing methods like this creates a new closure each time. Does anyone know why it does this? I can understand creating closures when defining a method body as we can use variables in an outer scope within the method body,

Groovy closure not work with static final field from super class

走远了吗. 提交于 2019-12-10 18:17:21
问题 class Parent { final static String newLine = "*" } class Child extends Parent{ List body = [3, 4, 5] String toString() { def str = new StringBuilder() body.each { str.append(it + newLine) } str } } def c = new Child() println c The above is one trivial sample to illustrate the problem. It couldn't be compiled using Groovy plugin on Eclipse . Remove either final or static in the field of super class solves the problem. However, I have no idea why it's the case. http://groovy.codehaus.org