This section is incomplete.
Shrinkable<T>
is a fundamental template class in RapidCheck. An instance of Shrinkable<T>
represents some value of type T
and provides a way to access all the ways in which this value can be shrunk. It has value semantics which means that it can be copied and passed around just like any other value. It has two member functions:
T value() const
returns the represented value of thisShrinkable
.Seq<Shrinkable<T>> shrinks() const
returns aSeq
(see the documentation forSeq
for more information) of all the possible ways of shrinking the value from the smallest to the largest.
There are two important things to note here:
value()
is a member function, not a member variable. This means that the way that theShrinkable
obtains the valueT
that it returns is abstracted away. If this wasn't the case, we would have to impose the following restrictions onT
:T
would need to have a copy constructor. Otherwise, we would only be able to retrieveT
once. This means we couldn't generate, for example,std::unique_ptr
s to objects. Non-copyable objects are very common in mainstream C++ code so this would be undesirable.T
's copy constructor would have to produce copies that shared no state with the original value. Without this guarantee, we would not be able to repeatedly provide semantically equivalent objects since the consumer ofT
might modify the copy which could also modify the original. The obvious example of a type which certainly doesn't provide the non-shared-state guarantee isstd::shared_ptr
whose entire purpose is having a copy constructor that yields copies with shared state.
shrinks()
returns aSeq
ofShrinkable<T>
and not justT
. This means that aShrinkable<T>
is a value ofT
combined with a tree of possible ways of shrinking it, not just a list. This allows RapidCheck to recursively search this for the smallest value value satisfying some condition (usually failing the property).