Reputation: 7042
With Scala 2.12, I can do this:
Try("3").fold(_.toString, _.toString)
but I can't this:
Try("3").fold(_.toString, _)
This is the error I receive:
missing parameter type for expanded function
Why a complicated expression does work and a simple one doesn't?
My workaround:
Try("3").fold(_.toString, x => x)
Upvotes: 0
Views: 57
Reputation: 369614
Your first snippet is equivalent to
Try("3").fold(x => x.toString, y => y.toString)
Your second snippet is equivalent to
x => Try("3").fold(y => y.toString, x)
This needs a type annotation, because the type of x
cannot be inferred from context.
If you want to pass the identity function, you can just pass the pre-defined identity
method from Predef
via η-expansion:
Try("3").fold(_.toString, identity _)
Or even implicit η-expansion:
Try("3").fold(_.toString, identity)
Upvotes: 4