映射函数不能 return void: tuple(__lambda3)

Mapping function(s) must not return void: tuple(__lambda3)

我有以下代码片段(见 Mark Isaacson's talk at DConf 2015

import std.stdio, std.range, std.algorithm;

void main(string[] args)
{
  bool[string] seen;
  bool keepLine(S)(S line){
    if(line in seen){
      return false;
    }
    seen[line.idup] = true;
    return true;
  }

  stdin
    .byLine
    .filter!(a => keepLine(a))
    .map!(a => a.writeln)
    .walk;
}

为什么会出现以下错误:

/usr/include/dmd/phobos/std/algorithm/iteration.d(476): Error: static assert  "Mapping function(s) must not return void: tuple(__lambda3)"
main.d(17):        instantiated from here: map!(FilterResult!(__lambda2, ByLine!(char, char)))
Failed: ["dmd", "-v", "-o-", "main.d", "-I."]

?

试试这个:

stdin
  .byLine
  .filter!(a => keepLine(a))
  .each!(a => a.writeln);

map 应该是 return 一系列值,因此它被更改为不再接受不 return 任何东西的函数。 each 的引入是为了替代像您这样的情况,其中谓词仅用于其副作用。