Lasf
Lasf

Reputation: 2582

How to convert an anonymous function to a method value?

With this code

val foo = List('a', 'b', 'c')
aString.forall(foo.contains(_))

IntelliJ highlights foo.contains(_) and suggests "Anonymous function convertible to method value". I have researched eta expansion, but am unable to see how I could improve this particular piece of code. Any ideas?

Upvotes: 16

Views: 2469

Answers (1)

Hugh
Hugh

Reputation: 8932

I believe it's saying that you could simply have

val foo = List('a', 'b', 'c')
aString.forall(foo.contains)

Note that we're not explicitly converting the foo.contains method here to an anonymous function.

Upvotes: 18

Related Questions