This page is no longer maintained — Please continue to the home page at www.scala-lang.org

scala Either type

3 replies
Jiansen
Joined: 2011-04-27,
User offline. Last seen 1 year 10 weeks ago.

Hi users,

Following is a tiny type safe program using the Either type.

1 object eitherTest extends App{
2 val i = Right(1)
3 if (i.isInstanceOf[Either[Int, Boolean]]) {
4 val b = i.asInstanceOf[Either[Int, Boolean]].right
5 if(b.get) {println("value "+i+" is a boolean!")}
6 }
7 }

Surprisingly, the test in Line 3 is evaluated to true and therefore
the program raises an exception due to the wrong casting at Line 4.

I don't understand why i:Right[Nothing,Int] should be an instance of
Either[Int, Boolean], any suggestions?

Jiansen

Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
Re: scala Either type
On Sat, Jan 7, 2012 at 7:38 AM, Jiansen <jiansenhe [at] gmail [dot] com> wrote:
Hi users,

Following is a tiny type safe program using the Either type.

1 object eitherTest extends App{
2  val i = Right(1)
3  if (i.isInstanceOf[Either[Int, Boolean]]) {
4     val b = i.asInstanceOf[Either[Int, Boolean]].right
5     if(b.get) {println("value "+i+" is a boolean!")}
6   }
7 }

Surprisingly, the test in Line 3 is evaluated to true and therefore
the program raises an exception due to the wrong casting at Line 4.

I don't understand why i:Right[Nothing,Int] should be an instance of
Either[Int, Boolean], any suggestions?

At runtime, the type parameters are erased.
http://stackoverflow.com/a/1338119/160378 http://stackoverflow.com/questions/1094173/how-do-i-get-around-type-erasure-on-scala-or-why-cant-i-get-the-type-paramete
However, you can use pattern matching to check the type of the *contents* of the Either.
object eitherTest extends App{
 val i = Right(1)   i match {    case Right(i: Int) => "Right containing an Int"    case Left(i: String) => "Left containing an String"    case _ => "something else"   }
}
-jason
milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: scala Either type

On Sat, Jan 7, 2012 at 6:48 AM, Jason Zaugg wrote:
> On Sat, Jan 7, 2012 at 7:38 AM, Jiansen wrote:
>>
>> Hi users,
>>
>> Following is a tiny type safe program using the Either type.
>>
>> 1 object eitherTest extends App{
>> 2  val i = Right(1)
>> 3  if (i.isInstanceOf[Either[Int, Boolean]]) {
>> 4     val b = i.asInstanceOf[Either[Int, Boolean]].right
>> 5     if(b.get) {println("value "+i+" is a boolean!")}
>> 6   }
>> 7 }
>>
>> Surprisingly, the test in Line 3 is evaluated to true and therefore
>> the program raises an exception due to the wrong casting at Line 4.
>>
>> I don't understand why i:Right[Nothing,Int] should be an instance of
>> Either[Int, Boolean], any suggestions?
>
>
> At runtime, the type parameters are erased.
>
> http://stackoverflow.com/a/1338119/160378
> http://stackoverflow.com/questions/1094173/how-do-i-get-around-type-eras...
>
> However, you can use pattern matching to check the type of the *contents* of
> the Either.

Alternatively you can use the Typeable type class from shapeless[1],

import Typeable._

val ri : Any = Right(1) // ri : Right[Nothing, Int]
val r1 = ri.cast[Either[Int, Boolean]] map (_.right) // r1 : Option[Boolean]
assertTrue(r1.isEmpty)

val rb : Any = Right(true) // rb : Right[Nothing, Boolean]
val r2 = rb.cast[Either[Int, Boolean]] map (_.right) // r2 : Option[Boolean]
assertTrue(r2.isDefined)

Cheers,

Miles

[1] http://www.chuusai.com/2011/12/19/shapeless-preview/

Cheers,

Miles

Lars Hupel
Joined: 2010-06-23,
User offline. Last seen 44 weeks 3 days ago.
Re: scala Either type

> Following is a tiny type safe program using the Either type.

Note that `asInstanceOf` makes any program not type safe. It's basically
saying that you think you know more than the compiler at this point.

> 1 object eitherTest extends App{
> 2 val i = Right(1)
> 3 if (i.isInstanceOf[Either[Int, Boolean]]) {
> 4 val b = i.asInstanceOf[Either[Int, Boolean]].right
> 5 if(b.get) {println("value "+i+" is a boolean!")}
> 6 }
> 7 }

This code should give you the following output from the compiler:

warning: there were 2 unchecked warnings; re-run with -unchecked for details

This warning is there for a reason. If you run the compiler with
`-unchecked`, observe this warning:

scala> Right(1).isInstanceOf[Either[Int, Boolean]]
:8: warning: non variable type-argument Int in type
Either[Int,Boolean] is unchecked since it is eliminated by erasure

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland