closures

C# - closures over class fields inside an initializer?

我怕爱的太早我们不能终老 提交于 2019-12-10 12:46:04
问题 Consider the following code: using System; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { var square = new Square(4); Console.WriteLine(square.Calculate()); } } class MathOp { protected MathOp(Func<int> calc) { _calc = calc; } public int Calculate() { return _calc(); } private Func<int> _calc; } class Square : MathOp { public Square(int operand) : base(() => _operand * _operand) // runtime exception { _operand = operand; } private int _operand; } } (ignore

Javascript closures on heap or stack?

夙愿已清 提交于 2019-12-10 12:43:57
问题 Where does JavaScript (according to the standard) store closures: heap or stack? Is there a third explicit place for closures? 回答1: In the end it is an implementation detail of the runtime. See Phoenix link As to implementations, for storing local variables after the context is destroyed, the stack-based implementation is not fit any more (because it contradicts the definition of stack-based structure). Therefore in this case closured data of the parent context are saved in the dynamic memory

Is it possible to return a reference from a closure in PHP?

陌路散爱 提交于 2019-12-10 12:38:03
问题 In order to return a reference from a function in PHP one must: ...use the reference operator & in both the function declaration and when assigning the returned value to a variable. This ends up looking like: function &func() { return $ref; } $reference = &func(); I am trying to return a reference from a closure. In in a simplified example, what I want to achieve is: $data['something interesting'] = 'Old value'; $lookup_value = function($search_for) use (&$data) { return $data[$search_for]; }

How does the memory management of closures in Scala work?

醉酒当歌 提交于 2019-12-10 12:30:11
问题 Scala allows closure like def newCounter = { var a=0 () => {a+=1;a} } which defines a function that on every call returns a new independent counter function starting at 1 : scala> val counter1 = newCounter counter1: () => Int = <function0> scala> counter1() res0: Int = 1 scala> counter1() res1: Int = 2 scala> val counter2 = newCounter counter2: () => Int = <function0> scala> counter2() res2: Int = 1 scala> counter1() res3: Int = 3 This is quite impressive as usually a would be a

jQuery/javascript events - prototype event handler

こ雲淡風輕ζ 提交于 2019-12-10 12:18:40
问题 The following code doesn't work as I intuitively expect it to: function MyObject(input) { input.change(this._foo); this.X = undefined; } MyObject.prototype._foo = function() { alert("This code is never called"); // but if it did this.X = true; } var test_input = $("input#xyz"); // a random, existing input var m = MyObject(test_input); // attach handler (or try to) test_input.change(); // trigger event alert(m.X); // undefined I'd expect that _foo() would be called (and, if that ever happens,

Groovy: how to call closure in top scope from another closure

﹥>﹥吖頭↗ 提交于 2019-12-10 10:45:11
问题 I'm trying to break up code that makes use of the Jenkins Job DSL plugin into reusable pieces, and I suspect that my question is generic to Groovy and not Jenkins-specific. For example, I want to reuse parts of this block: freeStyleJob() { //generic stuff name "something" description "something else" //custom stuff scm { svn { //etc.... } } } By placing name and description in a utility method (obviously I want to do more than just that in real life). However, I cannot find the proper syntax

Eclipse 4.2 and Java 8

一曲冷凌霜 提交于 2019-12-10 10:14:53
问题 I found this page stating that Java 8 support for Juno is deffered, but I can't find more information how soon people can exspect to be able to write first closures in Eclipse and get productive with that stuff. Has someone got insight how long we still have to wait? The Java7 features were in 3.7 really quickly, that's why it's kind of odd that this task is deferred. Any comments, ideas? Or maybe even a good workaround? 回答1: One of the key reasons that Java 8 support was deferred is that

Closure actions should be defined on controller

让人想犯罪 __ 提交于 2019-12-10 09:26:53
问题 Ember 1.13.10 I wanted to try out the closure actions, so I defined the a route: import Ember from 'ember'; export default Ember.Route.extend({ actions: { doSave() { ... } } }); and the template: {{my-component onSave=(action 'doSave')}} But I get the error message: An action named 'doSave' was not found in (generated test.index controller). However it is defined on the route. Given the fact that Controllers are kind of deprecated in Ember I would expect that the action should be defined on

Executing closure on Twig

拟墨画扇 提交于 2019-12-10 05:04:06
问题 I'm trying to execute a closure that resides inside an array on a Twig template. Below you could find a simplified snippet of which I'm trying: //Symfony controller ... $funcs = array( "conditional" => function($obj){ return $obj->getFoo() === $obj::TRUE_FOO } ); $this->render('template_name', array('funcs' => $funcs)); {# Twig template #} {# obj var is set #} ... {% if funcs.conditional(obj)%} <p>Got it</p> {% endif %} When Twig renders the template, throws an exception complaining about an

Debugging closures in javascript

徘徊边缘 提交于 2019-12-10 04:57:22
问题 When I try to debug the javascript code which has a lot of closures I use to put a breakpoints. Then I go to see the stack but most of the times I just see a call stack full of anonymous functions which is a nightmare for me. What is the best way to debug closure in javascript? 回答1: Well, in Google Chrome, you can see variables content throughout closures: Local is the current execution context Closure is its enclosing execution context ... Up to the global execution context 回答2: You can add