2012-07-28 152 views
0

在以下情况下,名称参数与功能发生冲突。名称参数与功能类型之间的不匹配

鉴于有些系列化基础设施:

// needs recursive access to itself. for reasons 
// beyond the scope of this questions, `self` must 
// be a by-name parameter 
class Transport(self: => Source[Transport]) 

// again: self is required to be by-name 
def transportSer(self: => Source[Transport]) : Serializer[Transport] = 
    new Serializer[Transport] { 
     def read(in: In)(implicit tx: Tx): Transport = new Transport(self) 
    } 

现在想象一个名叫Hook包装与递归/相互连接性交易:

trait Tx { 
    def readSource[A](implicit ser: Serializer[A]) : Source[A] = 
     new Source[A] { 
     def get(implicit tx: Tx): A = ser.read(new In {}) 
     } 
}  
trait In 
trait Source[A] { def get(implicit tx: Tx): A } 
trait Serializer[A] { def read(in: In)(implicit tx: Tx): A } 

和示例以及它的串行输入

trait Hook[A] { 
    def source: Source[A] 
} 

及其串行器:

def hookSer[A](peerSelf: Source[A] => Serializer[A]) : Serializer[Hook[A]] = 
    new Serializer[Hook[A]] { 
     def read(in: In)(implicit tx: Tx) : Hook[A] = 
     new Hook[A] with Serializer[A] { 
      val source: Source[A] = tx.readSource[A](this) 
      def read(in: In)(implicit tx: Tx) : A = peerSelf(source).read(in) 
     } 
    } 

那么下面失败:

val hs = hookSer[Transport](transportSer) 

<console>:15: error: type mismatch; 
found : => Source[Transport] => Serializer[Transport] 
required: Source[Transport] => Serializer[Transport] 
      val hs = hookSer[Transport](transportSer) 
            ^

如何这个问题能解决而不改变由名函数的参数(尽可能)?

回答

0

似乎可以写类型(=> Source[A]) => Serializer[A]

def hookSer[A](peerSelf: (=> Source[A]) => Serializer[A]) : Serializer[Hook[A]] = ... 

val hs = hookSer[Transport](transportSer) 
val h = hs.read(new In {})(new Tx {}) 
val t = h.source.get(new Tx {}) 
相关问题