Skip to content

Instantly share code, notes, and snippets.

@paulp
Last active August 29, 2015 13:56
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save paulp/9085763 to your computer and use it in GitHub Desktop.
Save paulp/9085763 to your computer and use it in GitHub Desktop.
import scala.collection.mutable.ListBuffer
class Ummutable[T] private (buf: ListBuffer[T]) {
def this(xs: T*) = this(ListBuffer() ++= xs)
def append(ys: T*): this.type = { buf ++= ys ; this }
final val xs: List[T] = buf.toIterable match { case xs: List[T] => xs }
override def toString = s"Ummutable(xs = ${xs mkString ", "})"
}
object Ummm {
def main(args: Array[String]): Unit = {
val um = new Ummutable(1, 2, 3)
println("um: " + um.xs)
um append (4, 5, 6)
println("um: " + um.xs)
}
// um: List(1, 2, 3)
// um: List(1, 2, 3, 4, 5, 6)
}
final case class UmmutableVarargs[T] private (xs: Array[T], ys: T*) {
def this(xs: Array[T]) = this(xs, xs: _*)
def update(idx: Int, value: T): this.type = { xs(idx) = value ; this }
override def toString = s"UmmutableVarargs(xs = ${xs mkString ", "}, ${ys mkString ", "})"
}
object UmmmVarargs {
def main(args: Array[String]): Unit = {
val um = new UmmutableVarargs(Array(1, 2, 3))
println("um: " + um)
um(1) = 55
println("um: " + um)
}
// um: UmmutableVarargs(xs = 1, 2, 3, 1, 2, 3)
// um: UmmutableVarargs(xs = 1, 55, 3, 1, 55, 3)
}
@pvorb
Copy link

pvorb commented Feb 20, 2014

What the...
🙈 🙉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment