Skip to content

Summon a generated instance without making it implicit #114

@koterpillar

Description

@koterpillar

I have a case class with a lot of members that yields well to generating an Arbitrary instance using scalacheck-shapeless, but some individual field values I'd like to discard as invalid.

For that purpose, I wanted to generate the instance using this library, call filter on it and make the result of that implicit.

I couldn't find anything in the documentation explaining how to generate an instance without making it implicit.

After some trial and error, I came up with:

implicit val arbitraryFoo: Arbitrary[Foo] = {
  val baseArbitrary = implicitly[Strict[MkArbitrary[Foo]]].value.arbitrary.arbitrary
  baseArbitrary.filter(validationCheck)
}

Is there a better way I'm missing?

If yes, it should be added to the documentation.

If not, this seems useful and can be implemented (see semiauto generic derivation in Circe as an example).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions