Last active
August 29, 2015 13:56
-
-
Save paulp/9085763 to your computer and use it in GitHub Desktop.
This file contains 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
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What the...
🙈 🙉