diff --git a/src/main/scala/org/scalacheck/Arbitrary.scala b/src/main/scala/org/scalacheck/Arbitrary.scala index 9b2dad90d..df06ba342 100644 --- a/src/main/scala/org/scalacheck/Arbitrary.scala +++ b/src/main/scala/org/scalacheck/Arbitrary.scala @@ -340,7 +340,7 @@ private[scalacheck] sealed trait ArbitraryLowPriority { /** Arbitrary instance of the Either type */ implicit def arbEither[T, U](implicit at: Arbitrary[T], au: Arbitrary[U]): Arbitrary[Either[T, U]] = - Arbitrary(oneOf(arbitrary[T].map(Left(_)), arbitrary[U].map(Right(_)))) + Arbitrary(Gen.either(at.arbitrary, au.arbitrary)) /** Arbitrary instance of the Future type */ implicit def arbFuture[T](implicit a: Arbitrary[T]): Arbitrary[Future[T]] = diff --git a/src/main/scala/org/scalacheck/Gen.scala b/src/main/scala/org/scalacheck/Gen.scala index dc2a83e14..4fdddd4eb 100644 --- a/src/main/scala/org/scalacheck/Gen.scala +++ b/src/main/scala/org/scalacheck/Gen.scala @@ -553,6 +553,10 @@ object Gen extends GenArities with GenVersionSpecific { def some[T](g: Gen[T]): Gen[Option[T]] = g.map(Some.apply) + /** Generates a `Left` of `T` or a `Right` of `U` with equal probability. */ + def either[T, U](gt: Gen[T], gu: Gen[U]): Gen[Either[T, U]] = + oneOf(gt.map(Left(_)), gu.map(Right(_))) + /** Chooses one of the given generators with a weighted random distribution */ def frequency[T](gs: (Int, Gen[T])*): Gen[T] = { val filtered = gs.iterator.filter(_._1 > 0).toVector