Search code examples
d

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


I have the following snippet (seen at 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;
}

Why would this give me the following error:

/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."]

?


Solution

  • Try this:

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

    map is supposed to return a range of values, so it was changed to no longer accept functions that don't return anything. each was introduced as a replacement for cases like yours, where the predicate is only used for its side effect.