Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"update" method with variable-length index in Scala

I was writing code for something like an array with variable dimensions. What I do is to maintain a linear underlying collections and wrap it up with index access methods. Since the dimension of the data structure is not known, I write something like

def apply(i: Int*): Double = ...

And it works perfectly. However, I cannot do the same thing to update method and operators like +=, so I end up writing methods like

def set(v: Double, i: Int*) ...
def add(v: Double, i: Int*) ...

which is fine but not what I really want. I guess the problem about update may be fixed in two ways:

  1. Change the order of arguments in update function, which makes it look weird.
  2. Allow variable-length arguments not as the last one. I find this questions asked in a general setting and it can be solved by using currying functions, which does not apply here.

The problem about += seems more complicated and it even exists when the index is of fixed length. Maybe we can add an object that has += operator and use this(...) to get the object (so that this(...) += v will invoke some method as we expect), but that will conflict with the apply method.

If anyone has solution to any of the above questions or has a reason why we shouldn't be able to write code like this, please share your ideas! Thanks~

like image 515
Kane Avatar asked Nov 16 '12 21:11

Kane


1 Answers

update is a fairly peculiar artefact in Scala because it is mainly syntactic sugar and doesn't correspond to any particular method signature. This means that we can be creative and give update an arity-polymorphic signature,

scala> class Indexed { def update[P <: Product](p: P) = p }
defined class Indexed

scala> val i = new Indexed
i: Indexed = Indexed@1ea0e836

scala> i(0) = 1.0
res0: (Int, Double) = (0,1.0)

scala> i(0, 1) = 1.0
res1: (Int, Int, Double) = (0,1,1.0)

scala> i(0, 1, 2) = 1.0
res2: (Int, Int, Int, Double) = (0,1,2,1.0)

scala> i(0, 1, 2, 3) = 1.0
res3: (Int, Int, Int, Int, Double) = (0,1,2,3,1.0)

As it stands, this leaves the types of the indices on the LHS and the type of the value on the RHS completely unconstrained,

scala> i(23, true, 'c') = "foo"
res4: (Int, Boolean, Char, String) = (23,true,c,foo)

but we can fix that with some implicit evidence provided by the new support for tuples in shapeless 2.0.0-SNAPSHOT,

scala> import shapeless._
import shapeless._

scala> import syntax.tuple._
import syntax.tuple._

scala> class Indexed {
     |   def update[P <: Product, I](p: P)
     |     (implicit
     |       init:   TupleInit.Aux[P, I],
     |       toList: TupleToList[I, Int],
     |       last:   TupleLast.Aux[P, Double]) = (toList(init(p)), last(p))
     | }
defined class Indexed

scala> val i = new Indexed
i: Indexed = Indexed@76ab909a

scala> i(0) = 1.0
res10: (List[Int], Double) = (List(0),1.0)

scala> i(0, 1) = 2.0
res11: (List[Int], Double) = (List(0, 1),2.0)

scala> i(0, 1, 2) = 3.0
res12: (List[Int], Double) = (List(0, 1, 2),3.0)

scala> i(0, 1, 2, 3) = 4.0
res13: (List[Int], Double) = (List(0, 1, 2, 3),4.0)

scala> i(0, 1, 2) = "foo" // Doesn't compile
<console>:22: error: could not find implicit value for parameter
  last: shapeless.ops.tuple.TupleLast.Aux[(Int, Int, Int, String),Double]
              i(0, 1, 2) = "foo" // Doesn't compile
                         ^

scala> i(23, "foo", true) = 5.0 // Doesn't compile
<console>:22: error: could not find implicit value for parameter
  toList: shapeless.ops.tuple.TupleToList[I,Int]
              i(23, "foo", true) = 5.0 // Doesn't compile
                                 ^
like image 50
Miles Sabin Avatar answered Sep 20 '22 12:09

Miles Sabin