Skip to content

Commit c3409dc

Browse files
committed
Improve docs for Arbitrary
1 parent cc8a268 commit c3409dc

File tree

1 file changed

+59
-30
lines changed

1 file changed

+59
-30
lines changed

src/main/scala/org/scalacheck/Arbitrary.scala

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,48 +17,77 @@ import scala.concurrent.duration.{Duration, FiniteDuration}
1717
import util.Buildable
1818
import util.SerializableCanBuildFroms._
1919

20-
21-
sealed abstract class Arbitrary[T] extends Serializable {
22-
def arbitrary: Gen[T]
23-
}
24-
25-
/** Defines implicit [[org.scalacheck.Arbitrary]] instances for common types.
26-
* <p>
27-
* ScalaCheck
28-
* uses implicit [[org.scalacheck.Arbitrary]] instances when creating properties
29-
* out of functions with the `Prop.property` method, and when
30-
* the `Arbitrary.arbitrary` method is used. For example, the
31-
* following code requires that there exists an implicit
32-
* `Arbitrary[MyClass]` instance:
33-
* </p>
20+
/**
21+
* Define an arbitrary generator for properties
22+
*
23+
* The [[Arbitrary]] module defines implicit generator instances for
24+
* common types.
25+
*
26+
* The implicit definitions of [[Arbitrary]] provide type-directed
27+
* [[Gen]]s so they are available for properties, generators, or other
28+
* definitions of [[Arbitrary]].
29+
*
30+
* ScalaCheck expects an implicit [[Arbitrary]] instance is in scope
31+
* for [[Prop]]s that are defined with functions, like [[Prop$.forAll[T1,P](g1*
32+
* Prop.forAll]] and so on.
33+
*
34+
* For instance, the definition for `Arbitrary[Boolean]` is used by
35+
* `Prop.forAll` to automatically provide a `Gen[Boolean]` when one
36+
* of the parameters is a `Boolean`:
3437
*
3538
* {{{
36-
* val myProp = Prop.forAll { myClass: MyClass =>
37-
* ...
39+
* Prop.forAll { (b: Boolean) =>
40+
* b || !b
3841
* }
42+
* }}}
3943
*
40-
* val myGen = Arbitrary.arbitrary[MyClass]
44+
* Thanks to `Arbitrary`, you don't need to provide an explicit
45+
* `Gen` instance to `Prop.forAll`. For instance, this is
46+
* unnecessary:
47+
*
48+
* {{{
49+
* val genBool: Gen[Boolean] = Gen.oneOf(true,false)
50+
* Prop.forAll(genBool) { (b: Boolean) =>
51+
* b || !b
52+
* }
4153
* }}}
4254
*
43-
* <p>
44-
* The required implicit definition could look like this:
45-
* </p>
55+
* Since an arbitrary `Gen` for `Boolean` is defined in `Arbitrary`,
56+
* it can be summoned with `Arbitrary.arbitrary` in cases where you
57+
* need to provide one explicitly:
4658
*
4759
* {{{
48-
* implicit val arbMyClass: Arbitrary[MyClass] = Arbitrary(...)
60+
* val genBool: Gen[Boolean] = Arbitrary.arbitrary[Boolean]
61+
* val genSmallInt: Gen[Int] = Gen.choose(0, 9)
62+
* Prop.forAll(genBool, genSmallInt) { (b: Boolean, i: Int) =>
63+
* i < 10 && b || !b
64+
* }
4965
* }}}
5066
*
51-
* <p>
52-
* The factory method `Arbitrary(...)` takes a generator of type
53-
* `Gen[T]` and returns an instance of `Arbitrary[T]`.
54-
* </p>
67+
* For a user-defined `MyClass`, writing the following requires that
68+
* there exists an implicit `Arbitrary[MyClass]` instance:
5569
*
56-
* <p>
57-
* The `Arbitrary` module defines implicit [[org.scalacheck.Arbitrary]]
58-
* instances for common types, for convenient use in your properties and
59-
* generators.
60-
* </p>
70+
* {{{
71+
* Prop.forAll { (myClass: MyClass) =>
72+
* ...
73+
* }
74+
* }}}
75+
*
76+
* The implicit definition of `Arbitrary[MyClass]` would look like:
77+
*
78+
* {{{
79+
* implicit val arbMyClass: Arbitrary[MyClass] = Arbitrary {
80+
* ...
81+
* }
82+
* }}}
83+
*
84+
* The factory method `Arbitrary(...)` expects a generator of type
85+
* `Gen[MyClass]` then it will return an instance of `Arbitrary[MyClass]`.
6186
*/
87+
sealed abstract class Arbitrary[T] extends Serializable {
88+
def arbitrary: Gen[T]
89+
}
90+
6291
object Arbitrary extends ArbitraryLowPriority with ArbitraryArities with time.JavaTimeArbitrary {
6392

6493
/** Arbitrary instance of the Function0 type. */

0 commit comments

Comments
 (0)