// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package jwj

class MyInt(val v: Int) {
  def +(o: MyInt) = new MyInt(v + o.v)
  def -(o: MyInt) = new MyInt(v - o.v)
  def *(o: MyInt) = new MyInt(v * o.v)
  def /(o: MyInt) = new MyInt(v / o.v)
  override def toString(): String = return s"$v"
}

object MyInt {
  implicit def Int2MyInt(v: Int): MyInt = new MyInt(v)
  implicit def MyInt2Int(v: MyInt): Int = v.v
}

class TenInt {
    val a = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
    def apply(ix: Int): Int = a(ix)
    def update(ix: Int, v: Int) { a(ix) = v }
}

object Helper {
  implicit class IntExtension(me: Int) {
    def plusPowerOf2(n: Int): Int = me + 1 << n
  }
}

object OO3 {
  def main(args: Array[String]): Unit = {
    val i1: MyInt = 1
    val i2: MyInt = 2
    val i3: MyInt = 3
    val i7: Int = i3 * i2 + i1
    println(i7)
    val ten = new TenInt()
    println(ten(7))
    ten(7) = 17
    println(ten(7))
    import jwj.Helper._
    println(1.plusPowerOf2(0))
    println(1.plusPowerOf2(1))
    println(1.plusPowerOf2(2))
    println(1.plusPowerOf2(3))
  }
}
