Abs*_*ype 2 tuples d variable-assignment multiple-return-values
传统上这是通过out
参数完成的,例如:
void notfun(ushort p, out ubyte r0, out ubyte r1)
{
r0 = cast(ubyte)((p >> 8) & 0xFF);
r1 = cast(ubyte)(p & 0xFF);
}
Run Code Online (Sandbox Code Playgroud)
使用元组可以将其重写为
auto fun(ushort p)
{
import std.typecons;
return tuple
(
cast(ubyte)((p >> 8) & 0xFF) ,
cast(ubyte)(p & 0xFF)
);
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,结果不能直接赋值给变量元组:
void main(string[] args)
{
ushort p = 0x0102;
ubyte a,b;
// ugly brute cast!
*(cast(ReturnType!(typeof(fun))*) &a) = fun(0x0102) ;
}
Run Code Online (Sandbox Code Playgroud)
是否有一种特殊的语法允许类似的东西
(a,b) = fun(0x0102);
Run Code Online (Sandbox Code Playgroud)
或任何其他惯用的方式来做类似的事情?
归档时间: |
|
查看次数: |
91 次 |
最近记录: |