Comments (4)
Essentially it is because those methods (all object methods) breaks parametricity which is a very important property to be able to reason about types and code. To quote @tel:
If I have a type BiFunction<A,A,A>
then with parametricity it’s either an infinite loop or one of the following
(x, y) -> x
or
(x, y) -> y
that’s a trivial example, but it scales very well and can eliminate huge swaths of potential implementations and vastly improve type-driven reasoning.
On the other hand, if you account for Object#hashcode/equals
, BiFunction<A,A,A>
has more inhabitants, even without considering impurity, for instance:
(x, y) -> if (x.hashCode > 1000) x else y
Other pitfalls of Object#equals
include:
- it let you compare
Apple
s andOrange
s. - there is usually no guarantees that it is not the default implementation inherited from
Object
which never what you want. - implementing it correctly in presence of subtyping is almost impossible.
For further reading/viewing on why you would want parametricity, I recommend Tony Morris' talks on the topic.
Hope it helps!
Edit (thanks @TomasMikula): the Bifunction<A, A, A>
is to be read in a context where A
is a universally quantified (unrestricted) type variable. Eg:
<A> BiFunction<A, A, A> foo() {
// either:
return (x, y) -> x
// or
return (x, y) -> y
}
from derive4j.
Thank you! I kept glossing over this part of the README and finally just had to ask :). I don't think I have the necessary background to actually understand your answer, but this gives me plenty of direction for further exploration. <3
from derive4j.
@jbgi I get your point, but BiFunction<A,A,A>
is a somewhat confusing type, because it doesn't make sense without an enclosing scope where A
is introduced. In that scope, A
might be an unrestricted type parameter (which is what you intended) as in
class Foo<A> {
BiFunction<A, A, A> f = ??? // this has to be one of
// (x, y) -> x
// (x, y) -> y
// infinite recursion
}
but there might also be more information available about A
in the enclosing scope, as in
class Foo<A extends Bar> {
BiFunction<A, A, A> f = ??? // many more options
}
abstract class Bar {
public int x;
}
So maybe instead of BiFunction
, it is better to use a generic method to illustrate the point:
static <A> A foo(A a1, A a2) {
// now my only options are
// return a1;
// return a2;
// infinite recursion, e.g. return foo(a1, foo(a2, a1));
}
from derive4j.
@TomasMikula totally, thanks for the clarification; I fail to mention that A
in my example need to be a universally quantified type variable for it to make sense.
from derive4j.
Related Issues (20)
- NoSuchElementException after upgrading from 0.10.2 to 1.1.1
- StackOverflow in derivingConfig when @Data annotation is used HOT 1
- Data constructors: allow referencing no yet generated types. HOT 1
- Extension APIs HOT 1
- Could not find instance derivator for fj.Equal and InstanceConfig(Optional.empty, Optional.empty) HOT 2
- @ExportAsPublic does not work correctly on (static) nested classes
- Add Vavr flavour HOT 4
- Improve lazy implementation to support stack-safe evaluation of nested Lazy values
- Ignoring arguments while pattern matching HOT 4
- Random bug : compilation pb with static imports of constructors HOT 4
- Supporting subtypes HOT 4
- Compilation times with Derive4J-generated code HOT 9
- Type classes derivation : wildcards are problematic HOT 2
- DSL example Pass compile but can't run . HOT 5
- Type classes derivation : arrays (or varargs) are problematic HOT 1
- Add Cycops flavour HOT 1
- Feature request: Support Java 11
- Any example about Lens support for VAVR? HOT 4
- Usage in mixed scala/java codebase
- More compact code for equals methods HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from derive4j.