-
-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
generated functional interface instances with Type parameters result are sometimes impure #73
Comments
Looks like a bug I’ll check it. |
I did some research. The problem you're observing here is not related to the type paramater but to the fact that 'A' also fits type
A generated function, however, does not store all results, but generates a hash from the parameters which is than used as a random seed to generate the actual value. This will generate an equal (but not same) object when called with the same parameters. You can mitigate the problem by making the type variable more specific, e.g. Storing all generated values would use up additional memory. I could implemented this as an optional feature. Would that be an important improvement for you? |
Having had one more night to think it over, I'd even say that always returning the exact same object would be wrong, ie. would leave out behaviour that can definitely happen. In a language like Java where you can have objects with identity and changing state it's common behaviour that functions or functional interfaces create those kind of objects - and create them freshly whenever they are being invoked. Consider for example all those factory methods we use in the context of Spring et al. If you want to constrain the type of objects to be created for
If you need those range of types regularly you might consider to register a default arbitrary provider. So I close this issue as "works as designed". Feel free to reopen it if you still see a bug or want to make a feature request out of it. |
I understand that this is the way Java objects behave, but it would nice be nice to be able to restrict or filter which types @Property
<@Eq A, @Eq B> void aInValuePos(
@ForAll Function<A, B> f,
@ForAll A a
) {
Assertions.assertThat(f.apply(a)).isEqualTo(f.apply(a));
} But I guess that would be essentially two different features. First the ability to register arbitrary providers on typeparameters and secondly the providers for Those are some pretty specific requirements though. I can understand if you don't think that this is something that you want to support. Although being able to annotate typeparameters would be nice regardless. |
The first required feature is rather simple since annotations are already being considered for the usage part of the type variable, thus
can be used for choosing an arbitrary. The providers for |
The advantage of declaring the Annotations at the variable declaration site would be that you could constrain all usages of that variable. It would also be kind of inconsistent. In this example: @Property
<A> void p(
@ForAll @Eq A a,
@ForAll A b
)... would Regarding the problem that the provider shouldn't know about |
I extracted part 1 into an issue of its own: #74 As for filtering arbitraries I haven't (yet) come up with an idea how to do that without breaking existing arbitrary providers or force them to know about a filter annotations. The problem is that neither |
If you generate instances of functional interfaces whose return type is a type variable of the property function then that instance is not pure.
Example:
The text was updated successfully, but these errors were encountered: