Skip to content

Instantly share code, notes, and snippets.

@Sciss
Created December 30, 2016 21:18
Show Gist options
  • Save Sciss/79922fcf3f18d9bb9d1ab703db92d0c8 to your computer and use it in GitHub Desktop.
Save Sciss/79922fcf3f18d9bb9d1ab703db92d0c8 to your computer and use it in GitHub Desktop.
import scala.collection.{GenTraversableOnce, LinearSeq, LinearSeqLike, mutable}
import scala.collection.generic.CanBuildFrom
object Message {
def apply[A](args: A*): Message[A] = new Message(args)
def newBuilder[A]: mutable.Builder[A, Message[A]] =
new mutable.ArrayBuffer[A].mapResult(buf => new Message(buf))
implicit def builderFactory[A]: CanBuildFrom[Any, A, Message[A]] = new CanBuildFrom[Any, A, Message[A]] {
def apply() = Message.newBuilder[A]
def apply(a: Any) = apply()
}
}
class Message[A] private (args: Seq[A])
extends LinearSeq[A] with LinearSeqLike[A, Message[A]] {
type Repr = Message[A]
def apply(idx: Int): A = args(idx)
def length: Int = args.length
override def iterator: Iterator[A] = args.iterator
override def newBuilder: mutable.Builder[A, Message[A]] = Message.newBuilder
}
Message(1, 2, 3) ++ Message(4, 5, 6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment