I am curious about the proper way of doing this. Suppose I want to signal success or failure from a method. Is this acceptable, if there is nothing I want to say in case of success, other than it succeeded?
def fn(): Either[Throwable, Unit]
And what is the proper way to return a Right() from this method, as a success? Apparently just returning Right() gives a deprecation warning (Adaptation of argument list by inserting () has been deprecated).
I can also probably do Option[Throwable], but that doesn't keep with the spirit of how I read Option. Or maybe return the result of scala.util.Try and evaluate Success/Failure?
-- On the deprecation warning, just FYI.
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_101).
Type in expressions for evaluation. Or try :help.
scala> def fn:Either[Throwable, Unit] = { Right() }
<console>:11: warning: Adaptation of argument list by inserting () has been deprecated: this is unlikely to be what you want.
signature: Right.apply[A, B](b: B): scala.util.Right[A,B]
given arguments: <none>
after adaptation: Right((): Unit)
def fn:Either[Throwable, Unit] = { Right() }
^
fn: Either[Throwable,Unit]
Instances of Either are either an instance of Left or Right. A common use of Either is as an alternative to Option for dealing with possible missing values. In this usage, scala. None is replaced with a Left which can contain useful information.
In Scala Either, functions exactly similar to an Option. The only dissimilarity is that with Either it is practicable to return a string which can explicate the instructions about the error that appeared.
Unit is a subtype of scala. AnyVal. There is only one value of type Unit , () , and it is not represented by any object in the underlying runtime system. A method with return type Unit is analogous to a Java method which is declared void . Source Unit.scala.
The unit is a return type in scala, and it acts as a void in another programming language like java. Now we will see one example to understand it better.
(Examples) Scala either is used to represent one of the two possible variables. It takes two parameters or we can say it has two child. These two child are described as left and right. Also, scala either is the alternative to scala Option. Also, it is used to deal with missing values.
1) If you want to define a function that does not return any value, then we should go for Unit in scala. 2) To make a function that does not return value, we have to use the Unit keyword there.
We use Either keyword with two possible values in it as a left and right child for success and failure. We can pass any type of data type as left and right inside the either in scala. Let’s see one practice syntax for a better understanding of it. In this simple way, we can define it. How Does Either Work in Scala?
You can use Unit
as type and return ()
.
def fn(): Either[Throwable, Unit] = Right(())
I would say that it depends on the kind of API that you are looking for.
def fn(): Either[Throwable, Unit]
sounds pretty non-idiomatic to me. If the main result you expect of the function is a side effect, then there doesn't seem to be a use in using an Either
. Did this come up because it's the terminal operation of a monadic operation chain?
It's become less popular and less idiomatic these days, but I would probably go with a Try[A]
and handle the cases there. The other idiomatic choice would be to, rather than shuffle exceptions in your Either
, to create a sealed trait hierarchy of all your error kinds and return those from your effectful function rather than raw Throwable
instances.
I'm not seeing the error you note when calling Right()
or Right( () )
at the REPL, what version of scalac
are you using?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With