gpt4 book ai didi

mixins - 使用 foreach 循环——无法读取变量

转载 作者:行者123 更新时间:2023-12-01 11:39:21 24 4
gpt4 key购买 nike

应该很简单,但事实并非如此。

这是我的代码:

string cases()
{
string ret = "";
string[] methods;

methods = [__traits(derivedMembers,mixin("Math"))];
foreach (string s; methods) ret ~= "case \"" ~ s ~ "\": return Math."~s~"(params);";

methods = [__traits(derivedMembers,mixin("OtherClass"))];
foreach (string s; methods) ret ~= "case \"" ~ s ~ "\": return OtherClass."~s~"(params);";

return ret;
}

string execute(string what, string[] params)
{
switch (what)
{
mixin(cases());
default: break;
}
return "";
}

我想做什么:

const string[] arrayWithClassNames = ["Math","SomeClass"];
foreach (string s; arrayWithClassNames)
{
methods = ...
foreach ...
}

很简单吧?问题是它提示说:

variable 's' cannot be read at compile time. 

有什么想法吗?

最佳答案

要创建编译时循环,您需要迭代一个元组。试试这个:

alias classNames = TypeTuple!("Math","SomeClass");
foreach (string s; classNames)
{
...
}

关于mixins - 使用 foreach 循环——无法读取变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23078009/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com