可变异构列表,Scala

时间:2013-10-24 10:12:17

标签: scala mutable shapeless heterogeneous

是否可以将元素添加到应该是异构列表的可变对象中?

所以我的意思是,我正在处理Dijkstra的算法,我需要以这种方式在图的弧中保留一些函数:

class Arc[M, N, T, K](var start: Node[M], var end: Node[N], var f: T => K)

其中start - 是一个起始节点; end - 结束节点; f - 是节点之间的某种功能,而Arc是保持此功能的对象,对我来说这是最重要的事情。

这一切都很完美,但我将所有弧保留在节点内的列表中:

class Node[T] (s: T) (implicit m: TypeTag[T]) {
    var transitions: List[Arc[T, _, _, _]] = Nil
    //...
    def addArc[M, N, L] (end: Node[M], f: N => L): Arc[T, _, _, _] = {
        transitions = new Arc(this, end, f) :: transitions
        transitions.head
    }
}

你知道,问题在于我们在简单列表中丢失类型;在为节点添加新弧时,我们将丢失有关类型的所有信息。

解决问题的可能方法是使用HLists;一些工作代码的示例(为了向exaplnation添加更多信息):

// for example we got some type convertion functions
def doubleToInt(x: Double) = x.toInt
def doubleToString(x: Double) = x.toString
def stringToInt(x: String) = x.toInt
def intToDouble(x: Int) = x.toDouble  

// and we got some class, with two any args and one function; 
class testyfunc[M, N, K, T] (a: M, b: N, c: K => T) {
  var f: K => T = c(_)
}

// ok lets build HList
var omglist = HNil
var omglist1 = (new testyfunc (1, "234", intToDouble)) :: omglist
var omglist2 = (new testyfunc (1, "234", doubleToInt)) :: omglist1
var omglist3 = (new testyfunc (1, "234", doubleToString)) :: omglist2
var omglist4 = (new testyfunc (1, "234", stringToInt)) :: omglist3

// it all works!
println(omglist4.head.f("223")) // > 223

// lest check types; yeah it's perfect!
/* shapeless.::[main.scala.Main.testyfunc[Int,java.lang.String,String,Int],
   shapeless.::[main.scala.Main.testyfunc[Int,java.lang.String,Double,java.lang.String],
   shapeless.::[main.scala.Main.testyfunc[Int,java.lang.String,Double,Int],
   shapeless.::[main.scala.Main.testyfunc[Int,java.lang.String,Int,Double],
   shapeless.HNil]]]] */
顺便说一下,现在一切都是这样的:

var omglistNH: List[testyfunc[_, _, _, _]] = Nil // here we loose types!
omglistNH = (new testyfunc (1, "234", intToDouble)) :: omglistNH
omglistNH = (new testyfunc (1, "234", doubleToInt)) :: omglistNH
omglistNH = (new testyfunc (1, "234", doubleToString)) :: omglistNH
omglistNH = (new testyfunc (1, "234", stringToInt)) :: omglistNH

println(omglistNH.head.f("223")) 
// obviously we got a type error; cause of incorrect types
/* type mismatch;
 found   : String("223")
 required: _$9 where type _$9
 println(omglistNH.head.f("223"))
                       ^          */

所以问题是如何像下面的代码一样制作smth(它是错误的 - 类型错误),并向这个HList添加元素:

var omglist = HNil
omglist = (new testyfunc (1, "234", intToDouble)) :: omglist
omglist = (new testyfunc (1, "234", doubleToInt)) :: omglist
omglist = (new testyfunc (1, "234", doubleToString)) :: omglist
omglist = (new testyfunc (1, "234", stringToInt)) :: omglist

因为我甚至不知道如何传递这种类型不匹配,并且我认为没有办法将所有转换保持为节点的字段并保留所有类型。

修改

hm,mb我可以将转换传递给构造函数,而不是使用可变对象。

0 个答案:

没有答案
相关问题