So if you had, say, a List<Foo>, and you had an Equal<Foo> instance
available, you would want your List equality to use that. But .equals
has no way of knowing about it.
So I propose a compromise. How about a wrapper? This wrapper would
take three arguments in its constructor: List<A>, Equal<A>, Hash<A>.
It could implement .equals and .hashCode because it has all the
information it needs.
I'm not terribly bothered by not working the way IntelliJ and Lombok
expect. But it would be nice to be able to use an fj.data.List as,
say, a key in a java.util.HashMap, and this requires implementing
.equals and .hashCode. Having a wrapper like described above would
accomplish this.
What do you think of that?
Runar
> --
> You received this message because you are subscribed to the Google Groups "Functional Java" group.
> To post to this group, send email to functio...@googlegroups.com.
> To unsubscribe from this group, send email to functionaljav...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/functionaljava?hl=en.
>
Sure. You'd want a constructor that takes an Equal<A>, Hash<A>, and
List<A>. That sounds pretty easy.
> 2. Would it be difficult to create/maintain?
Not nearly as difficult as .equals and .hashCode. To quote my friend
Daniel Spiewak: "There is no more heinous crime against humanity" than
these two methods.
> 3. Would it integrate with existing functionaljava collections? For
> instance, we would probably want a WrappedList.map() to return a WrappedList
> instead of a List.
Or just: `wrap(wrapped.list.map(f))` to unwrap and rewrap. I'd be fine
with that, although having delegations to List's methods would be fine
too.
> This argument doesn't seem convincing because the Java API requires that
> every object must implement an equals method, and hence you can perform an
> equality test on any two objects at runtime.
This does not seem convincing to me because, well, it actually does
not require it. And besides, it assumes that there is only one
implementation of equality for any given type. Basically .equals,
.hashCode, and .toString are a design flaw in the standard library.
The fact that we are having this conversation demonstrates this flaw.
> What is the probability that existing code would break by the addition of
> List.equals()?
Very small. But it would encourage the writing of poorly factored code.
> If we do proceed with a wrapper, then I would also recommend adding a
> ToString<A> instance to it, so that it can have a nice default toString
> implementation.
Good idea.
Just out of curiosity, what is it that you need the .equals and
.hashCode methods for, specifically? You said you were surprised by
their absence. Were you looking for them out of academic curiosity, or
was there some code that didn't work properly because they weren't
there?
Runar
Just out of curiosity, what is it that you need the .equals and
.hashCode methods for, specifically? You said you were surprised by
their absence. Were you looking for them out of academic curiosity, or
was there some code that didn't work properly because they weren't
there?
No, that's not what I meant at all. But if we do add them, they should
be compatible with List's Equal instance. So the implementation should
be the same as:
https://github.com/functionaljava/functionaljava/blob/master/core/src/main/java/fj/Equal.java#L247
Feel free to add .equals and .hashCode this way and submit a pull
request. If you're feeling generous, add them to Stream, NonEmptyList,
Tree, etc. as well. :)
Runar