Skip to content Skip to sidebar Skip to footer

@composite Vs Flatmap In Complex Strategies

hypothesis allows two different ways to define derived strategies, @composite and flatmap. As far as I can tell the former can do anything the latter can do. However, the implement

Solution 1:

@composite and .flatmap are indeed exactly equivalent - anything you can do with one you can also do with the other, and it will have the same performance too.

I actually wrote that comment, and the reason is that we only sometimes want to use a flatmap/composite, but always want to carefully validate our logic. The way I've set it up, we can avoid calling the validators more than once by using .flatmap - which would require a second function definition if we wanted to use @composite.

(there's also an issue of API style in that those arguments are almost always values, but can sometimes be strategies. We now ban such APIs based largely on the confusion arrays() has caused, in favor of letting users write their own .flatmaps)

Post a Comment for "@composite Vs Flatmap In Complex Strategies"