Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions core/src/main/java/fj/Ord.java
Original file line number Diff line number Diff line change
Expand Up @@ -514,4 +514,21 @@ public int compare(A o1, A o2) {
public Comparator<A> toComparator() {
return new OrdComparator();
}

/**
* Given two Ord instances that compare the given type of object, chain them together so
* that the result of the first is returned if it is not EQ, otherwise the result
* of the second is returned. This can be used to reuse an Ord between subclasses
* or implementations of an interface.
*
* @return An Ord instance combining the two provided Ord instances
*/
public static <A> Ord<A> chain(Ord<? super A> ord1, Ord<? super A> ord2) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my understanding, functionaljava try to avoid the use of variance annotations (and subtyping as a whole), except for methods specifically created to deal with subtyping, like fj.Function.vary(F<? super A, ? extends B>).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the variance annotation you can't directly reuse an Ord for a
superclass in a subclass, I think. This is exactly why I created this
chain function, was to reuse Ord in subclasses.

On Mon, May 25, 2015, 1:43 AM JB Giraudeau notifications@github.com wrote:

In core/src/main/java/fj/Ord.java
#139 (comment)
:

@@ -514,4 +514,21 @@ public int compare(A o1, A o2) {
public Comparator toComparator() {
return new OrdComparator();
}
+

  • /**
  • * Given two Ord instances that compare the given type of object, chain them together so
  • * that the result of the first is returned if it is not EQ, otherwise the result
  • * of the second is returned. This can be used to reuse an Ord between subclasses
  • * or implementations of an interface.
  • * @return An Ord instance combining the two provided Ord instances
  • */
  • public static Ord chain(Ord<? super A> ord1, Ord<? super A> ord2) {

In my understanding, functionaljava try to avoid the use of variance
annotations (and subtyping as a whole), except for methods specifically
created to deal with subtyping, like fj.Function.vary(F<? super A, ?
extends B>).


Reply to this email directly or view it on GitHub
https://github.com/functionaljava/functionaljava/pull/139/files#r30969203
.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You cannot 'directly' reuse it but you can still use comap which should do what you want at a small cost:

    Ord<Date> dateOrd = Ord.comparableOrd();
    Ord<Timestamp> timestampOrd = dateOrd.comap(t -> t);

functionaljava does not support variance annotation because composition is usually preferred (over inheritance) when functional-programming.

return ord(a -> a2 -> {
Ordering cmp = ord1.compare(a, a2);
if(cmp != Ordering.EQ)
return cmp;
return ord2.compare(a, a2);
});
}
}