我有一个类创建 aList<Action<int>>并将其保留到稍后的时间。此类可以在此列表中添加和删除委托。只要人们不太花哨,这种方法就很有效。为了对抗匿名函数(无法删除),我检查委托的目标是否为空。如果它为空,我会抛出异常。当存在包含函数的匿名委托时,就会出现问题。它有一个目标,但同样不可移动。下面的简化代码说明了我的问题
public class MyDelegateContainer
{
List<Action<int>> m_Container = new List<Action<int>>();
public void Add(Action<int> del)
{
if (del.Target == null)
{
throw new Exception("No static handlers");
}
m_Container.Add(del);
}
public bool Remove(Action<int> del)
{
if (m_Container.Contains(del))
{
m_Container.Remove(del);
return true;
}
return false;
}
}
public class MyFakeActionClass
{
public void Test(int temp) { }
}
class Program
{
static void Main(string[] args)
{
bool removed = false;
int counter = 0;
MyDelegateContainer container = new MyDelegateContainer();
MyFakeActionClass …Run Code Online (Sandbox Code Playgroud) 我猜想没有办法在匿名函数中获取函数调用者名称,是吗?
(function()
{
var cls = function()
{
this.foo = function()
{
console.log(arguments.callee.caller); // null
foo1();
}
var foo1 = function()
{
console.log(arguments.callee.caller); // foo
foo2();
}
var foo2 = function()
{
console.log(arguments.callee.caller); // foo1
cls.foo(); // local
}
var cls =
{
foo : function()
{
console.log(arguments.callee.caller); // cls.foo2
}
}
}
return (window.cls = cls);
})();
var c1 = new cls();
c1.foo();
Run Code Online (Sandbox Code Playgroud) 所以我的目标是创建一串随机字母,并且字母可以在字符串中重复。所以我想我可以聪明地这样做:
$str = implode(
array_fill(0,10,
function(){
$c='abcdefghijklmnopqrstuvwxyz';
return (string)$c{rand(0,strlen($c)-1)};
}
)
);
echo $str;
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
可捕获的致命错误:类 Closure 的对象无法转换为字符串...
这实际上是我的脚本中唯一的东西,所以不,它不是其他东西。现在,手册规定第三个参数描述array_fill是“用于填充的值”,并且它被列为接受混合类型。现在我知道“混合”不一定等于“任何”类型,但对我来说,我应该能够使用匿名函数作为第三个参数,只要它返回一个字符串,这似乎是合理的,对吗?但显然我不能这样做..
所以,我不一定要问为什么我不能这样做;我只是问为什么我不能这样做。这很可能归结为当权者没有将其写入幕后的代码中。但我想我只是想仔细检查一下我是否“正确”执行了这段代码,因为如果 php 允许(但不允许),它“应该”工作,而不是也许我在其他地方搞砸了?
如果我有下面的代码,我将两个函数作为参数传递给 function sayHi,这是回调的示例吗?
我注意到有两种运行这些“参数函数”的方法:要么如下所示,我们在定义它们的地方调用函数(作为参数),要么在 sayHi 函数中调用参数。这是回调函数和匿名函数之间的区别吗?
function sayHi(name, testForTrue) {
if (testForTrue == true) {
console.log(name);
}
}
sayHi(function() {
return 'Zach'
}(), function() {
return true;
}());
Run Code Online (Sandbox Code Playgroud)
我可以获得相同结果的另一种方法如下。在这种情况下,我在不同的时间评估函数?两者之间有什么实际区别吗?
function sayHi(name, testForTrue) {
if (testForTrue() == true) {
console.log(name());
}
}
sayHi(function() {
return 'Zach'
}, function() {
return true;
});
Run Code Online (Sandbox Code Playgroud) PHP 5.3+ 支持匿名函数(尽管在 PHP 7.x+ 中,它对匿名函数的支持在绑定方面略有不同)。我正在运行 PHP 5.6.x
是否有一种语法允许将多个参数传递给用作回调的匿名函数(而不是仅使用数组)。以下哪些示例(如果有)可以在 PHP 中实现?
实施例1
function ($str1, $str2 ){ //But, that would be too easy, right?
return $str1 . $str2;
}
Run Code Online (Sandbox Code Playgroud)
实施例2
function () use ($string, $min, $max) { // Not seeing this in the manual.
$length = mb_strlen($string, 'UTF-8');
return ($length >= $min) && ($length <= $max);
}
Run Code Online (Sandbox Code Playgroud)
实施例3
只是出于好奇,这种形式可能吗?
function ($str1, $str2 ) use ($int1, $int2) { // But, that would be in the …Run Code Online (Sandbox Code Playgroud) 对于 PHP 中的 LINQ,我使用了https://github.com/Athari/YaLinqo
我不知道如何在where子句中传递变量。
public function filter($arr, $find) {
Enumerable::from($arr)->where(function($val) { return stripos($val->item, $find) > -1; })->toArray();
}
Run Code Online (Sandbox Code Playgroud)
似乎没有像$find未定义那样工作,但我将它作为方法的参数发送。
我有一个我创建的 Swift 函数,它接受一个函数作为参数:
func doMath(_ f:(_ i1 : Int, _ i2 : Int) ->
(), _ i1: Int, _ i2: Int) {
print("doing Math")
f(i1, i2)
}
Run Code Online (Sandbox Code Playgroud)
该函数采用两个参数(均为 Int)并且不返回任何内容。
我可以使用非匿名函数通过以下代码成功调用该方法。
func add(_ m1:Int, _ m2: Int){
print (m1 + m2)
}
doMath(add, 3,5)
Run Code Online (Sandbox Code Playgroud)
当 doMath 被调用时,它会打印:
做数学
然后调用 add 函数,然后打印:
8
什么语法允许doMath使用匿名函数调用函数?
我试过的
doMath(5,8, {
(m1:Int, m2:Int) -> () in
print(m1 * m2)
})
Run Code Online (Sandbox Code Playgroud)
我收到一个错误,指出:
我正在研究将launch协程代码作为block: suspend CoroutineScope.() -> Unit. 我们通常将代码作为 lambda 传递。但是,我想知道如何将此函数作为显式参数传递给启动函数。
coroutineScope {
launch(block = ::myFunction)
}
suspend fun CoroutineScope.myFunction(): Unit {
// coroutine code
}
Run Code Online (Sandbox Code Playgroud)
它给出以下错误
Type mismatch.
Required:
suspend CoroutineScope.() ? Unit
Found:
KSuspendFunction0<Unit>
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
这在 C# 中可能吗?以下代码产生编译器错误。
HashSet<Task<(string Value, int ToNodeId)>> regionTasks =
new HashSet<Task<(string Value, int ToNodeId)>>();
foreach (Connection connection in Connections[RegionName])
{
regionTasks.Add(async () =>
{
string value = await connection.GetValueAsync(Key);
return (value, connection.ToNode.Id);
}());
}
Run Code Online (Sandbox Code Playgroud)
C# 编译器抱怨“错误 CS0149:应为方法名称。” 无法推断 lambda 方法的返回类型。
请注意我在 lambda 块关闭后立即通过 () 调用 lambda 方法的技术 {}。这确保Task返回 a ,而不是 a Func。
VB.NET 编译器理解这种语法。我惊讶地发现 VB.NET 编译器胜过 C# 编译器的示例。有关完整故事,请参阅我的An Async Lambda Compiler Error Where VB Outsmarts C#博客文章。
Dim regionTasks = New HashSet(Of Task(Of (Value As String, …Run Code Online (Sandbox Code Playgroud) 我在这里找到了在 Perl 中重命名匿名订阅者的解决方案。它涉及临时修改符号表以插入所需的名称。此解决方案使用要替换的硬编码符号表名称。我的问题是我想在运行时动态选择符号表名称。像这样的东西:
$pkg = 'MyPkg::ModA::';
$name = 'subname';
...
no strict 'refs';
local *{"${pkg}__ANON__"} = "$name [anon]";
strict refs;
Run Code Online (Sandbox Code Playgroud)
使其工作的唯一方法是禁用严格引用。如果它们未被禁用,脚本将失败并显示以下消息:
不能使用字符串 ("MyPkg::ModA::__ANON__") 作为符号引用,而在 /path/to/source/File.pm 行 xx 处使用“严格引用”
请注意,可以使用等效语句
local ${$pkg}{__ANON__} = "$name [anon]";
Run Code Online (Sandbox Code Playgroud)
带有类似的错误消息:
不能使用字符串 ("MyPkg::ModA::") 作为 HASH 引用,而在 /path/to/source/File.pm 行 xx 处使用“严格引用”
是否可以在不禁用严格引用的情况下做同样的事情?
TMI/DNR:
如果您感兴趣,这里有一个完整的示例。具有讽刺意味的是,我的解决方案使用匿名子重命名给定的匿名子。
ModA.pm
package MyPkg::ModA;
use strict;
use warnings;
use MyPkg::Util;
# Create a new instance.
sub new
{
my ($type, $class, $self);
# allow for both ModA::new and $moda->new
$type = shift; …Run Code Online (Sandbox Code Playgroud) php ×3
c# ×2
callback ×2
javascript ×2
asynchronous ×1
delegates ×1
kotlin ×1
linq ×1
parameters ×1
perl ×1
reference ×1
strict ×1
swift ×1
symbol-table ×1
syntax ×1
task ×1
vb.net ×1
yalinqo ×1