closures

Passing closure to trait method: expected type parameter, found closure

别来无恙 提交于 2019-12-22 10:34:11
问题 I'm a bit stumped on how to get this working, I've cut it down from the real thing. I wrote a trait: pub trait Renderable<F: Fn(&PropertyTags)> { fn set_property_changed_callback(&mut self, callback: Option<F>); } Which the 'child' parameter of add_child is restricted by and PropertyTags is just an enum. I've included mock implementations of the type of child to demonstrate my usage: pub struct Child<F: Fn(&PropertyTags)> { property_changed_callback: Option<F>, } impl<F: Fn(&PropertyTags)>

When is this scope/closure being garbage collected in javaScript?

橙三吉。 提交于 2019-12-22 10:24:00
问题 I am doing a course which is going through scope/closures and briefly mentions garbage collection. During the course a question is posed: How long does the scope stay around? And the answer was—until there's no longer any references to it. Yep, so what we basically said was, a closure is kind of like a reference to a hidden scope object. So as long as there's some function that still has a closure over the scope, that scope's going to stay around. But as soon as that closure goes away, scope

Javascript private methods — what is the memory impact?

爷,独闯天下 提交于 2019-12-22 10:09:18
问题 I'm working on a bit of code where I'm attempting to hide some private variables inside closures. The thing is the environment is fairly constrained in terms of memory, so I'm also concerned with keeping the overall footprint of the classes low. What is the impact of using closures to hide private instance variables and methods when compared to just making all methods and variables on an object public? Would an instance of the one using closures take up more memory than an instance that did

closure requires unique access

眉间皱痕 提交于 2019-12-22 09:58:13
问题 I'm trying to avoid repeating myself by using a closure in the following code: fn add_raw(&mut self, pair: RawLinkPair) { let convert = |raw: &RawLink| { Link{ id: self.get_or_create(raw.name).id, flow: raw.flow, } }; println!("Hive received pair: {}", pair); let parent = convert(&pair.parent); let child = convert(&pair.child); self.link_concepts(parent, child); } That doesn't work. It gives me this error: hive.rs:64:9: 64:13 error: cannot borrow `*self` as mutable because previous closure

Inject variable into callback function scope

微笑、不失礼 提交于 2019-12-22 09:02:15
问题 Is this possible to add variable to callback scope? What I want to achieve is: ... Foo.prototype.bar = function(fn) { var baz = "baz!"; fn.call(this); } ... Foo.bar(function() { console.log(baz) // gives "baz!" }); I know I can pass baz variable as an argument or this but I'm interested in something like above. 回答1: No, it's not possible. The only ways are the ones you pointed out: as an argument or in this . 回答2: What about doing it this way: var Foo = function(){} Foo.prototype.handle =

Understanding Java's approximation to “closures”

可紊 提交于 2019-12-22 08:37:52
问题 So, here's what I understand: Java doesn't support closures, so it sort of copies the variables from the containing scope into the nested scope, so they are available later. Because this is a copy, there is no way to synchronize the original and the copy, and the variable is forced to be final so the developer cannot change it and expect it to be updated. This understanding is partly taken form these answers And that brings us to this code working: public class SimpleClosure { public static

using jQueryUI with closure compiler

依然范特西╮ 提交于 2019-12-22 08:24:07
问题 I am having trouble getting a js app that uses jQuery UI to work after minifying using the closure compiler. What I did: Go here and load up the jqueryui js file Asked to extern jQuery.ui Copied the result to a file and used it as an extern file The app broke, though. The dialogs do not show correctly anymore. The explosion effect doesn't work correctly, and there are several dialogs created. It is interesting that jQuery UI itself is working somewhat, since the dialogs were created. It is

Google Closure event delegation a'la jQuery live/on

雨燕双飞 提交于 2019-12-22 08:18:17
问题 I need to delegate event to newly created elements, I need to attach handler to their creation event. Something similar to: onCreate I do not want to bind the event to the element after the creation by addressing it: jQuery: $(element).click(function(){}); I would prefer something like $.on('document','spawn', '.item', function(e) { if (e.target == this.target) { alert('element created: '+this); } }); Is there a way to do so in google closure? the goal is to attach the event on creating and

JavaScript: self-calling function returns a closure. What is it for?

梦想与她 提交于 2019-12-22 05:45:10
问题 Studying one JavaScript library I found following construction: theMethod: function () { var m1 = new SomeClass(); return function (theParameter) { this.someMethod(); m1.methodCall(this.someField1); this.someField2 = 'some value'; } }() theMethod is called as follows: c.theMethod(paramValue); What did the author want to say with this declaration? Why not to use such declaration: theMethod: function (theParameter) { var m1 = new SomeClass(); this.someMethod(); m1.methodCall(this.someField1);

Critique of immutable classes with circular references design, and better options

耗尽温柔 提交于 2019-12-22 05:33:41
问题 I have a factory class that creates objects with circular references. I'd like them to be immutable (in some sense of the word) too. So I use the following technique, using a closure of sorts: [<AbstractClass>] type Parent() = abstract Children : seq<Child> and Child(parent) = member __.Parent = parent module Factory = let makeParent() = let children = ResizeArray() let parent = { new Parent() with member __.Children = Seq.readonly children } [Child(parent); Child(parent); Child(parent)] |>