首页 > 解决方案 > 如何使用 Dlang 在范围块中编写多条语句?

问题描述

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

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

解决方案


只需使用稍长的形式函数语法:

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; }.


推荐阅读