9,053 questions
-2
votes
2
answers
97
views
How to use Perl `eval` to build a closure that executes the command in a string
In a CGI script that processes arrays of strings the user can supply for each field whether to match a string there or not.
If all user-specified criteria are fulfilled, the specific array should be ...
1
vote
1
answer
86
views
Why doesn’t new Function have access to local variables in JavaScript? [duplicate]
I was experimenting with closures in JavaScript and ran into something confusing.
function outer() {
let x = 10;
return new Function("return x;");
}
const fn = outer();
console.log(fn());
I ...
2
votes
0
answers
103
views
Does closure variable capture introduce thread safety risks due to field implementation?
Follow-up to: Detailed Explanation of Variable Capture in Closures
Given that captured variables are implemented as fields in compiler-generated classes (as explained in the referenced answer), does ...
5
votes
1
answer
75
views
Why does this code that moves a closure into another closure and returns both even compile?
I have the following code:
fn test() -> (impl FnMut(&mut u8), impl FnMut(&mut u8)) {
let a = |v: &mut u8| {*v = 0};
let b = move |v: &mut u8| { a(v); println!("{}&...
-2
votes
1
answer
71
views
Will new object assignment to same GLOBAL variable cause memory leaks?
I'm trying to set GLOBAL variable available across all modules of Node.js app.
module.exports = app => {
app.get('/config', async (req, res) => {
.....
const { data } = await axios....
0
votes
0
answers
35
views
Why does V8 deoptimize certain closures with inline caching despite seemingly monomorphic access patterns?
I'm debugging a performance regression in a hot code path and found that V8 is deoptimizing a function that uses closures in a monomorphic way. Here’s a simplified version of the pattern
function ...
1
vote
1
answer
57
views
Lua stack and vararg functions
I'm curently exploring Lua (5.4.7) vm implementation, it's relatively simple but i can't figure our how return works in case of vararg functions.
functions with fixed amount of params are quite simple,...
0
votes
0
answers
52
views
Statically compile Groovy DelegatingScript with delegated object containing method with closure
I have following classes
final class GroovyValidator {
@Override
public List<String> validate(String script) {
final CompilerConfiguration config = new CompilerConfiguration();
...
0
votes
1
answer
59
views
Create mutable iterator over a set of indices?
I'm trying to implement an array where items can occupy multiple slots (details not relevant to question).
I have created a method that allows me to iterate over a range of items:
pub fn get(&self,...
0
votes
0
answers
26
views
Understanding liftimes for multithreading in Rust [duplicate]
I have a data structure that needs to be read for tasks in multiple threads. Such a function looks like
fn parallel_monte_carlo<T, R>(
rngs: &mut [&mut R],
initial_state: &[T]...
0
votes
2
answers
105
views
Why is the second character missing and an undefined character added in my typing effect? [duplicate]
I'm working on a simple typing effect component in React using Framer Motion. Here's the code for my TypingText component
TypingText.jsx
import { motion } from 'framer-motion';
import React, { ...
1
vote
1
answer
91
views
Is an FnMut argument a reference?
I've been trying to understand closures better. I originally wrote
fn main() {
let mut x = 0;
let f = || x += 1;
call_me(f);
f();
println!("x = {x}");
}
fn call_me<F&...
2
votes
1
answer
96
views
What's the difference between `T: Trait + 'a` and `impl Trait + 'a` in Rust?
I originally thought T: 'a will restrict you form using T instance outside 'a because this exmaple reports an error:
fn main() {
let num;
{
let num1 = 10; // error: num1 doesn't live ...
1
vote
2
answers
58
views
Closure over numeric for-loop variable in Lua
I have a question about Lua, closures and local variables.
From my understanding, the loop variable in a numerical for-loop is implicitly local so I was expecting the following code to behave ...
1
vote
2
answers
95
views
How does React's useCallback read the variable in closures [duplicate]
In the following example, when the button is clicked, cb2 uses the memoized function. However, why doesn’t cb2 use the first render closures (countVal: 0)?
function TestHook() {
const [count, ...
2
votes
3
answers
78
views
question related to use setInterval and useEffect, what's the difference between pass function name and anonymous function?
Recently I am reading article related to useInterval, these code works fine:
import React, { useState, useEffect, useRef } from "react";
import ReactDOM from "react-dom";
function ...
0
votes
1
answer
32
views
PHPunit risky test with Unintentionally Covered Code in wordpress callbacks
I am writing tests that execute some of WordPress hooks / actions
My hook example
add_filter("asdasd", static function() {
echo 'asdf';
});
It's complaining:
This test executed code ...
0
votes
0
answers
21
views
Why isn't FeedMessage.decode.bind(FeedMessage) equivalent to x => FeedMessage.decode(x) in Javascript? [duplicate]
Below is a Javascript code which attempts to parse some protobuf input data.
'use strict';
import protobuf from 'protobufjs';
const proto = protobuf.loadSync('gtfs-realtime.proto');
const ...
0
votes
1
answer
92
views
Why closure works like this in Python?
I don't quite understand how closure works in Python. It behaves in an unexpected way that I can't comprehend.
def A():
b = 1
def B():
b += 1
return b
return B()
print(A())
...
0
votes
1
answer
114
views
How are closures implemented in Rust?
In many languages closures are implemented using a structure in combination with a standard associated function (with a fixed name) which provides a method of making the object "callable".
...
-1
votes
1
answer
91
views
Swift Closure Capture List without the "in" keyword
does anybody know where this is documented? All the places I searched for, closures require a in keyword after the capture list but there is a valid case in which you can just specify the capture list....
1
vote
1
answer
107
views
What is the different of Box<closure> and closure?
let mut x = 10;
let mut closure: Box<dyn FnMut() -> i32> = Box::new(|| {
println!("x = {}", x);
x += 5;
x
});
let value1 = closure(); // x = 10
let value2 = closure(); /...
1
vote
2
answers
891
views
Swift 6 error: non-sendable type 'Timer' in a `@Sendable` closure
In Xcode 16, I get this error, related to Swift 6.
Capture of 'timer' with non-sendable type 'Timer' in a @Sendable
closure; this is an error in the Swift 6 language mode
How can I make this code ...
0
votes
1
answer
96
views
Why does Rust consider an owning closure to exist for 'static lifetime?
I was considering the possibility of passing a closure that owns some thread-safe value, to a spawned thread. The thread would then be able to call something only knowing the signature, while the ...
1
vote
1
answer
100
views
@Output in child component is undefined when trying to emit Angular v17
I have a list component displaying objects with accordion elements.
On expanding the accordion body, I use a (click)="addToArray(id)" method and have <edit-component *ngIf="idArray....