Scala: how to understand the flatMap method of Try?

后端 未结 6 2463
情深已故
情深已故 2021-02-20 05:29

The flatMap method of the Success is implemented like this:

  def flatMap[U](f: T => Try[U]): Try[U] =
    try f(value)
    catch {
      case NonFatal(e) =&g         


        
6条回答
  •  清酒与你
    2021-02-20 06:16

    You may consider Try[T] as similar to a collection of only one element (like Option[T]) .

    When the "sequence of sequences" is "only one sequence", map and flatmap are almost similar. Only difference being the signature of the function.

    No flattening is required in this case.

提交回复
热议问题