Skip to content

Instantly share code, notes, and snippets.

@jroper
Created June 4, 2012 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jroper/2869212 to your computer and use it in GitHub Desktop.
Save jroper/2869212 to your computer and use it in GitHub Desktop.
Scala would be more fun if it had one of these...
final case class Schrödinger[+A, +B](a: A, b: B) extends Either[A, B] {
private var isleft = true
private var isright = true
lazy val actual : Either[A, B] = {
if (new Random().nextBoolean()) {
isright = false
Left(a)
} else {
isleft = false
Right(b)
}
}
def isLeft = isleft
def isRight = isright
def left = Either.LeftProjection(actual)
def right = Either.RightProjection(actual)
}
@jedws
Copy link

jedws commented Jun 4, 2012

needs moar laziness:

class Schrödinger[+A, +B](private a: => A, private b: => B)

and you can make isLeft/isRight lazy vals too:

lazy val isLeft = actual.isLeft
lazy val isRight = actual.isRight

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment