Skip to content

Instantly share code, notes, and snippets.

@paulp
Created February 20, 2015 18:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulp/9faded2a9a98f97d2a6b to your computer and use it in GitHub Desktop.
Save paulp/9faded2a9a98f97d2a6b to your computer and use it in GitHub Desktop.
package object jtos {
type uV = scala.annotation.unchecked.uncheckedVariance
type CanBuildFrom[-From, -Elem, +To] = generic.CanBuildFrom[From, Elem, To]
type ~>[-T, +R] = juf.Function[_ >: T, _ <: R]
type Consumer[-T] = juf.Consumer[_ >: T]
type Function[-T, +R] = juf.Function[_ >: T, _ <: R]
type Predicate[-T] = juf.Predicate[_ >: T]
type ToDoubleFunction[-T] = juf.ToDoubleFunction[_ >: T]
type ToIntFunction[-T] = juf.ToIntFunction[_ >: T]
type ToLongFunction[-T] = juf.ToLongFunction[_ >: T]
type Comparator[-T] = ju.Comparator[_ >: T]
type StringJoiner = ju.StringJoiner
type Characteristics = jus.Collector.Characteristics
// Convenience only
type jArray[A] = Array[A with Object]
type Folder[A, -B] = BiFunction[A, _ >: B, A]
// Type aliases don't work for attaching covariance to a java type:
// https://issues.scala-lang.org/browse/SI-8079
//
// So these still have to be annotated with @uV at every site.
type jStream[+T] = jus.Stream[T @uV]
type jSpliterator[+T] = ju.Spliterator[T @uV]
type jOption[+T] = ju.Optional[T @uV]
type jIterator[+T] = ju.Iterator[T @uV]
def jNone[A]: jOption[A] = ju.Optional.empty[A]
def jSome[A](x: A): ju.Optional[A] = ju.Optional.of(x)
// Some invariance explanations:
type UnaryOperator[T] = juf.UnaryOperator[T] // T => T Legitimately invariant
type BinaryOperator[T] = juf.BinaryOperator[T] // (T, T) => T Legitimately invariant
type IntFunction[R] = juf.IntFunction[R] // Int => R Should be +R, defeated by Stream#toArray
type BiFunction[T, -U, R] = juf.BiFunction[T, _ >: U, R] // (T, U) => R Should be -T -U +R, partially defeated by Stream
type Supplier[R] = juf.Supplier[R] // () => R Should be +R, defeated by Stream#collect
type BiConsumer[T, U] = juf.BiConsumer[T, U] // (T, U) => Unit Should be -T -U, defeated by Stream#collect
type Collector[-T, A, R] = jus.Collector[_ >: T, A, R] // (A, T) => A and A => R.
// The signatures of collect taint the potential covariance of Supplier and Collector.
// <R> R collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner);
// <R, A> R collect(Collector<? super T, A, R> collector);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment