以下声明:
const(string[char]) AA1 = [
'a' : "fkclopel",
'b' : "poehfftw"
];
void main(string args[]){}
Run Code Online (Sandbox Code Playgroud)
给我:
C:...\temp_0186F968.d(1,27):错误:非常量表达式['a':"fkclopel",'b':"poehfftw"]
而它适用于其他类型.
专用于内置阵列的范围原语消耗它们的源,但是可以很容易地设计一个范围系统,而不是基于.ptr源的范围系统(首先看起来更灵活).
struct ArrayRange(T)
{
private T* _front;
private T* _back;
this(ref T[] stuff) {
_front = stuff.ptr;
_back = _front + stuff.length;
}
@property bool empty(){return _front == _back;}
@property T front(){ return *_front;}
@property T back(){return *_back;}
void popFront(){ ++_front;}
void popBack(){--_back;}
T[] array(){return _front[0 .. _back - _front];}
typeof(this) save() {
auto r = this.array.dup;
return typeof(this)(r);
}
}
void main(string[] args)
{
auto s = "bla".dup;
// here the source is 'fire-proofed'
auto rng = …Run Code Online (Sandbox Code Playgroud) 我希望我的自定义错误消息显示而不是系统生成的错误消息.具体来说,在这种情况下,如果用户试图打开已经在使用的文件,我想捕获错误并显示我的消息,但系统总是打败我,并生成以下消息:"项目.. .raised异常类EFOpenError,消息'无法打开文件'文件路径和名称".进程无法访问该文件,因为它正被另一个进程使用'." 当我关闭此错误消息时,即显示我的消息.我想只显示我的信息.我不希望显示系统消息.有没有办法做到这一点?我的代码不起作用.
这是我的代码:
begin
//check if input file can be opened
try
if OpenDialog1.Execute then
begin
Application.ProcessMessages;
Memo1.Clear;
try
Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
InputFile := TStringList.Create;
InputFile.LoadFromFile(OpenDialog1.FileName);
ActiveFileName := ExtractFileName(OpenDialog1.FileName);
lblActiveFileName.Caption := 'Active File: ' + ActiveFileName;
mmuDisplayClear.Enabled := True;
mmuAnalysisOptions.Enabled := True;
except on
EFOpenError do //if file does not exist or is in use
MessageDlgPos('File either does not exist or may be ' +
'in use!', mtError, [mbOK], 0, 300, 300);
end;
end;
finally
mmuDispInstructions.Enabled := True;
mmuDispData.Enabled := False; …Run Code Online (Sandbox Code Playgroud) 传统上这是通过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)
或任何其他惯用的方式来做类似的事情?