两个...就是型列上场时.
void assignTuple(S, T...)(auto ref S s, auto ref T t)
{
static foreach (i; 0 .. S.length)
t[i] = s[i];
}
void main()
{
import std;
string a,b;
tuple("a", "b").assignTuple(a,b);
}
安不安全?
import std;
auto _(T...)(return ref T refs) @safe {
static struct Assigner(Ptrs...) {
@disable this(this);
private Ptrs ptrs;
void opAssign(T)(T v) if (T.expand.length == Ptrs.length)
{
static foreach (i; 0 .. Ptrs.length)
*ptrs[i] = v[i];
}
}
static Assigner!U assigner(U...)(U v) {
return Assigner!U(v);
}
static string assignerCall(size_t len)() {
string ret = "assigner(";
static foreach (i; 0 .. len)
ret ~= "&refs[" ~ i.stringof ~ "], ";
return ret ~ ")";
}
return mixin(assignerCall!(T.length));
}
void main()
{
string a, b, c;
int x;
_(a, b, c, x) = tuple("a", "b", "c", 4);
writeln(a, b, c, x);
_(a, b) = tuple(b, a);
writeln(a, b);
}
有点奇怪:
struct Version {
template opDispatch(string name) {
mixin("version ("~name~") enum opDispatch = true; else enum opDispatch = false;");
}
}
static if (Version.Windows) pragma(msg, "Windows machine");
static if (Version.linux) pragma(msg, "Linux machine");
看看:
enum SymbolKind {
S_YYEMPTY = -2,
S_YY_EOF = 0,
S_YYerror = 1,
}
import std;
pragma(msg, format("%d", SymbolKind.S_YYEMPTY));
pragma(msg, format("%s", SymbolKind.S_YYEMPTY));
再看看:
string overrideState(string s)
{
// work your magic here, it's normal D code!
}
void foo(string s)()
{
mixin(overrideState(s));
}
浙公网安备 33010602011771号