Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.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
如何在Scala中声明一个返回自身的函数(或另一个具有相同签名的函数)?_Scala - Fatal编程技术网

如何在Scala中声明一个返回自身的函数(或另一个具有相同签名的函数)?

如何在Scala中声明一个返回自身的函数(或另一个具有相同签名的函数)?,scala,Scala,首先是背景-我正在编写一些代码来解码从套接字接收到的消息。套接字的性质意味着这些消息(通过testring作为接收)可能是完整的、被截断的或与其他消息连接在一起的。消息本身被分解成一个公共头,其中包含后面变量数据的长度 我的计划是让函数读取消息的各个部分,例如,第一次读取可以读取变量数据的长度,第二次读取变量数据本身 当调用其中一个方法时,我希望它返回下一个操作,该操作可能是读取序列中的下一个逻辑操作,如果由于尚未接收到所需的所有数据而无法执行特定的读取,则返回该操作本身 如果从套接字接收的数据

首先是背景-我正在编写一些代码来解码从套接字接收到的消息。套接字的性质意味着这些消息(通过testring作为
接收)可能是完整的、被截断的或与其他消息连接在一起的。消息本身被分解成一个公共头,其中包含后面变量数据的长度

我的计划是让函数读取消息的各个部分,例如,第一次读取可以读取变量数据的长度,第二次读取变量数据本身

当调用其中一个方法时,我希望它返回下一个操作,该操作可能是读取序列中的下一个逻辑操作,如果由于尚未接收到所需的所有数据而无法执行特定的读取,则返回该操作本身

如果从套接字接收的数据是完全帧化的,则读取操作将在
readLength
readData
之间交替进行。但是,当从套接字中以多个块读取数据时,读取操作可能遵循
readLength
readData
readData
模式

希望这是有道理的

我想做的是这样的事情(由于循环引用而无法编译):

这样我就可以声明如下内容:

def readLength(buffer: ByteBuffer): ReadFunction = {

  if (buffer.limit - buffer.position > 4) {

    return readData(buffer.getInt)
  }
  else {

    return readLength
  }
}

def readData(length: Int)(buffer: ByteBuffer): ReadFunction = {

  if (buffer.limit - buffer.position > length) {

    val data: Array[Byte](size)
    buffer.get(data) 

    //process data

    readLength   
  }
  else {

    readData(length)
  }
}
implicit class ReadFunction(f: Int => ReadFunction) extends (Int => ReadFunction) {
  def apply(i: Int) = f(i)
}

lazy val read: ReadFunction = { i: Int => println(i); read }

scala> read(1)(2)(3)
1
2
3
res0: ReadFunction = <function1>
现在我知道我可以(实际上已经)通过将读取操作定义为类来解决这个问题,这些类扩展了一个公共特性,并返回每个特性的实例,但这似乎与Scala不太一样


所以我的问题是-如何定义一个可以返回自身的函数?

您不能在
类型
声明中使用循环引用,但可以在
特征
声明中使用循环引用,如下所示:

def readLength(buffer: ByteBuffer): ReadFunction = {

  if (buffer.limit - buffer.position > 4) {

    return readData(buffer.getInt)
  }
  else {

    return readLength
  }
}

def readData(length: Int)(buffer: ByteBuffer): ReadFunction = {

  if (buffer.limit - buffer.position > length) {

    val data: Array[Byte](size)
    buffer.get(data) 

    //process data

    readLength   
  }
  else {

    readData(length)
  }
}
implicit class ReadFunction(f: Int => ReadFunction) extends (Int => ReadFunction) {
  def apply(i: Int) = f(i)
}

lazy val read: ReadFunction = { i: Int => println(i); read }

scala> read(1)(2)(3)
1
2
3
res0: ReadFunction = <function1>
隐式类ReadFunction(f:Int=>ReadFunction)扩展(Int=>ReadFunction){
def应用(i:Int)=f(i)
}
lazy val read:ReadFunction={i:Int=>println(i);read}
scala>读取(1)(2)(3)
1.
2.
3.
res0:ReadFunction=

是的,这正是我最终得到的结果——因为我是Scala的新手,我想知道纯函数是否也可以做到这一点:-)