diff options
author | Geoffrey Washburn <geoffrey.washburn@epfl.ch> | 2008-04-24 07:39:25 +0000 |
---|---|---|
committer | Geoffrey Washburn <geoffrey.washburn@epfl.ch> | 2008-04-24 07:39:25 +0000 |
commit | c0b21797bde81861305dd68853add2d8bd46e484 (patch) | |
tree | 45f2a138b05614beaa1a8b331fcadc255df97743 /src/library | |
parent | dcfbdbfd10868cff05a085dc132516d8e5f1c011 (diff) | |
download | scala-c0b21797bde81861305dd68853add2d8bd46e484.tar.gz scala-c0b21797bde81861305dd68853add2d8bd46e484.tar.bz2 scala-c0b21797bde81861305dd68853add2d8bd46e484.zip |
Changed isLeft and isRight to use less memory.
Changed either from a sealed trait to a sealed abstract class to allow
exhaustiveness checking.
All changes per the discussion in #797.
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/Either.scala | 16 |
1 files changed, 5 insertions, 11 deletions
diff --git a/src/library/scala/Either.scala b/src/library/scala/Either.scala index 70c755b932..b01a1a9a41 100644 --- a/src/library/scala/Either.scala +++ b/src/library/scala/Either.scala @@ -12,7 +12,7 @@ package scala * @author <a href="mailto:research@workingmouse.com">Tony Morris</a>, Workingmouse * @version 1.0, 11/10/2008 */ -sealed trait Either[+A, +B] { +sealed abstract class Either[+A, +B] { /** * Projects this <code>Either</code> as a <code>Left</code>. */ @@ -42,18 +42,12 @@ sealed trait Either[+A, +B] { /** * Returns <code>true</code> if this is a <code>Left</code>, <code>false</code> otherwise. */ - lazy val isLeft = this match { - case Left(_) => true - case Right(_) => false - } + def isLeft = false // Default here, overriden in Left /** * Returns <code>true</code> if this is a <code>Right</code>, <code>false</code> otherwise. */ - lazy val isRight = this match { - case Left(_) => false - case Right(_) => true - } + def isRight = false // Default here, overriden in Right. } /** * The left side of the disjoint union, as opposed to the <code>Right</code> side. @@ -61,14 +55,14 @@ sealed trait Either[+A, +B] { * @author <a href="mailto:research@workingmouse.com">Tony Morris</a>, Workingmouse * @version 1.0, 11/10/2008 */ -final case class Left[+A, +B](a: A) extends Either[A, B] +final case class Left[+A, +B](a: A) extends Either[A, B] { override def isLeft = true } /** * The right side of the disjoint union, as opposed to the <code>Left</code> side. * * @author <a href="mailto:research@workingmouse.com">Tony Morris</a>, Workingmouse * @version 1.0, 11/10/2008 */ -final case class Right[+A, +B](b: B) extends Either[A, B] +final case class Right[+A, +B](b: B) extends Either[A, B] { override def isRight = true } object Either { /** |