如何使用Dlang在范围块中编写多条语句?

问题描述 投票:1回答:1

我想在范围块中编写多条语句,如下所示:

long[] W = [0L];
long[] V = [0L];

array.each!(s => // "s" has following strings "3 4" 
  W ~= s.split(" ")[0].to!long;
  V ~= s.split(" ")[1].to!long;
);

但是这会导致编译错误。有什么办法可以写出范围内的所有语句?

d
1个回答
2
投票

只需使用稍长的表单函数语法:

long[] W = [0L];
long[] V = [0L];

array.each!( (s) {
    W ~= s.split(" ")[0].to!long;
    V ~= s.split(" ")[1].to!long;
  }
);

[(s) { x; y; z; }适用于任何s => x适用的地方,如果需要返回值,则使用s=>x除外,长格式为(s) { return x; }

© www.soinside.com 2019 - 2024. All rights reserved.