Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala to Java8 stream compatibility issue

(scala)

Files.walk(Paths.get("")).forEach(x => log.info(x.toString))

gives

Error:(21, 16) missing parameter type
  .forEach(x => log.info(x.toString))
           ^

and (java8)

Files.walk(Paths.get("")).forEach(x -> System.out.println(x.toString()));

works fine

What's wrong?

like image 510
Eevy Avatar asked Jan 06 '23 17:01

Eevy


1 Answers

stream.forEach(x -> foo()) in java is syntactic sugar for

stream.forEach(
  new Consumer<Path> { public void accept(Path x) { foo(); } }
)

This is not at all the same as x => ... in scala, which is an instance of Function[Path,Unit].

Try this;

Files.walk(Paths.get(""))
  .forEach(new Consumer[Path] { def accept(s: Path) = println(s) })
like image 118
Dima Avatar answered Jan 09 '23 19:01

Dima