Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function 按名称调用参数与函数类型不匹配_Function_Scala_Lazy Evaluation_Callbyname - Fatal编程技术网

Function 按名称调用参数与函数类型不匹配

Function 按名称调用参数与函数类型不匹配,function,scala,lazy-evaluation,callbyname,Function,Scala,Lazy Evaluation,Callbyname,在以下场景中,按名称参数会导致与函数冲突 给定一些序列化基础结构: 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

在以下场景中,按名称参数会导致与函数冲突

给定一些序列化基础结构:

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 }
示例类型及其序列化程序:

// 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)
   }
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)
         }
   }
现在想象一个名为
Hook
的包装器,它处理递归/交互连接:

trait Hook[A] {
   def source: Source[A]
}
及其序列化程序:

// 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)
   }
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)
                                      ^
val hs=hookSer[Transport](transportSer)
:15:错误:类型不匹配;
找到:=>源[传输]=>序列化程序[传输]
必需:源[传输]=>序列化程序[传输]
val hs=挂钩器[传输](传输器)
^

如何在不将名称参数更改为函数(尽可能)的情况下修复此问题?

似乎可以编写类型
(=>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 {})