How to return a module from scala function?

I try to make the return Unit function (this is to implement the RxScala observer), but when I add () to the end, I get the error "Application does not accept parameters." Here is my code:

 val client3MessageStreamObserver: Observable[Message] = client3.messageStream() client3MessageStreamObserver.subscribe( m => println("Unexpected message received by client3"), // callback for handling exceptions t => println("Ex client3: " + t) // want to make this line work (which it doesn't) which is why // I need to be able to return Unit. // client3TestPromise.success(true) () // error after adding Unit literal here. ) 

Why am I getting this error after adding () and how can I get rid of it? If I leave this, I get the error message "Type of mismatch: Expected (Throwable) => Unit, actual: (Throwable) => Any)".

+6
source share
2 answers

Try the following:

 val client3MessageStreamObserver: Observable[Message] = client3.messageStream() client3MessageStreamObserver.subscribe( m => println("Unexpected message received by client3"), t => println("Ex client3: " + t) () => () ) 

The third onCompleted function is the Unit => Unit function. Thus, the parameter () , and then in return, we can explicitly return () or any method that returns () , for example println .

+3
source

OK, so I dealt with this. Since subscribe expects functions as arguments, I needed to wrap a few instructions in curly braces to make a block of code, i.e.:

 val client3MessageStreamObserver: Observable[Message] = client3.messageStream() client3MessageStreamObserver.subscribe( // single instruction function doesn't require braces m => client3TestPromise.failure(new RuntimeException("Unexpected " + "message received by client3")), // multi-instruction function does require braces t => { println("Ex client3: " + t) client3TestPromise.success(true) } ) 
0
source

Source: https://habr.com/ru/post/987906/


All Articles