Created
April 2, 2017 04:07
-
-
Save paulp/bb9251f7e37f4d6c2ee99ea6fb2cd486 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package policy | |
import scala.reflect.ClassTag | |
trait Iterator[@specialized +A] extends scala.Iterator[A] { | |
def next(): A | |
def hasNext: Boolean | |
} | |
object Iterator { | |
def wrap[A](it: scala.Iterator[A]): Iterator[A] = new Iterator[A] { | |
def next(): A = it.next | |
def hasNext = it.hasNext | |
} | |
} | |
trait Varargs[@specialized +A] { | |
def toArray: Array[Any] | |
def seq: Seq[A] | |
def length: Int | |
def apply(idx: Int): A | |
def foreach(f: A => Unit): Unit = iterator foreach f | |
def iterator: Iterator[A] = Iterator wrap seq.iterator | |
} | |
object Varargs { | |
final val emptyArray: Array[Any] = new Array[Any](0) | |
implicit def unwrapVarargs[A](xs: Varargs[A]): Seq[A] = xs.seq | |
implicit def wrapVarargs[A](xs: scala.collection.Seq[A]): Varargs[A] = new VarargsSeq[A](xs) | |
def toScala[A](): Varargs[A] = EmptyVarargs | |
def toScala[A](xs: Varargs[A]): Varargs[A] = xs | |
def toScala[A](xs: Array[A]): Varargs[A] = new VarargsArray(xs) | |
def toScala[A](xs: scala.collection.Seq[A]): Varargs[A] = new VarargsSeq(xs) | |
def toJava[A](): Array[Any] = emptyArray | |
def toJava[A](xs: Varargs[A]): Array[Any] = xs.toArray | |
def toJava[A](xs: Array[A]): Array[Any] = xs.asInstanceOf[Array[Any]] | |
def toJava[A](xs: scala.collection.Seq[A]): Array[Any] = xs.toArray[Any] | |
} | |
final object EmptyVarargs extends Varargs[Nothing] { | |
def toArray = Varargs.emptyArray | |
def seq = Nil | |
def apply(idx: Int): Nothing = sys error "empty.apply" | |
def length: Int = 0 | |
} | |
final class VarargsArray[@specialized +A](xs: Array[A]) extends Varargs[A] { | |
def toArray = xs.asInstanceOf[Array[Any]] | |
def seq: scala.collection.Seq[A] = xs | |
def apply(idx: Int): A = seq(idx) | |
def length: Int = seq.length | |
} | |
final class VarargsSeq[+A](xs: scala.collection.Seq[A]) extends Varargs[A] { | |
def toArray = xs.toArray[Any] | |
def seq = xs | |
def apply(idx: Int): A = xs(idx) | |
def length: Int = xs.length | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment