1

How does one pattern match Success or Failure on this method:

trait FunctionApi:
     def computeTry(funct: Try[String] => Unit): Unit = ??? // some ops

def actUponTry(functionApi: FunctionApi): String = {

     // This below could be something other than match as 
     // long as `Success` or `Failure` is apparent)

     functionApi.computeTry(...) match
     // When Try is ... (get Try + exception or value)
     // Success(value) => // Act on Success & value
                  s"Success with $value" 
     // Failure(ex) =>    // Act on Failure & ex 
                  s"Failure with exception ${ex.getString}"

}

What if FunctionApi has another method called computeTry2 is computeTry2(funct: Try[Long] => Unit): Unit, can this "match test" be more generic?

Do not use external libraries.

Maybe How to pattern match a function in Scala? is helpful?

Maybe there is a way to use another wrapper trait or method to get extract the Success / Failure params?

ADDITIONAL EDIT:

The ultimate goal is to assign a Promise to enact a Future. (I want to do this part on my own.) The calling code could look like this:

val success = Success("Some String")
val succeeding = new FunctionApi:
    def computeTry(continuation: Try[String] => Unit): Unit =
        continuation(success)
val wasSuccess = actUponTry(succeeding).computeTry()
    

EDIT #2: I managed to get this to work - I'll close this issue as the issue as this evolved.

  def actUponTry(functionApi: FunctionBasedApi): StringBasedApi = {
    class StringBasedApiX extends StringBasedApi {
      def computeTry(): String = {
        functionApi.computeTry {
          case Success(value) => s"Success with $value"
          case Failure(ex) => s"Failure with exception ${ex.getMessage}"
        }
      }
    }
    new StringBasedApiX
  }

/**
  * Dummy example of a callback-based API
  */

trait FunctionBasedApi:
  def computeTry(funct: Try[String] => String): String

  /**
   * API similar to [[CallbackBasedApi]], but based on `String` instead
   */
trait StringBasedApi:
  def computeTry(): String

with test code:

  test("test matching") {
    val success = Success("Some String")
    val stringBasedApi = new FunctionBasedApi:
      def computeTry(funct: Try[String] => String): String = funct(success)
    val wasSuccess = actUponTry(stringBasedApi).computeTry()
    assertEquals(wasSuccess,  s"Success with ${success.get}")
  }

But I found that the way I was trying to do this with futures works differently than the above code (the above ... I could not get to pattern match).

8
  • 2
    I don't follow what the question / problem is? And the code doesn't make sense at all. Commented Apr 29 at 17:46
  • 2
    Can you describe with words what you're trying to do and an example of code of expected usage of all this? computeTry returns Unit, there's nothing to pattern match on. Commented Apr 29 at 18:36
  • 2
    I would love to be more specific with my question, but really, I just don't understand what you are asking. Can you elaborate more on what the problem is? What are you trying to express in the code? What have you tried and didn't work? Commented Apr 29 at 19:23
  • 1
    It's still unclear to me. In your edit you call computeTry without parameter. Where does continuation should come from? Commented Apr 29 at 20:09
  • 1
    @codeaperature so if I am understanding correctly, the whole question is how to implement the function that will be passed to computeTry? And that function needs to pattern match on the Try that it receives as the input, right? Commented Apr 29 at 20:55

1 Answer 1

1

Maybe you're looking for

def actUponTry(functionApi: FunctionApi): Unit = {
  functionApi.computeTry {
    case Success(str) => ???
    case Failure(e)   => ???
  }
}

https://scastie.scala-lang.org/DmytroMitin/Axi9gMbXRImB9qlonWDEWA/1

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.