Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.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/1/visual-studio-2008/2.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 Buffereditor内部函数中的增量头_Scala - Fatal编程技术网

Scala Buffereditor内部函数中的增量头

Scala Buffereditor内部函数中的增量头,scala,Scala,嗨,我看到了我认为在scala中的奇怪行为。在BufferEditor上调用head似乎是在内部函数中增加head。要么我的计算是错误的,在这种情况下,为什么输出是正确的。还是输出错误 给定: import scala.io.Source val source = Source.fromString("abcdef") val buff1 = source.buffered; println("outer head 1: " +buff1.head) println("outer head

嗨,我看到了我认为在scala中的奇怪行为。在BufferEditor上调用head似乎是在内部函数中增加head。要么我的计算是错误的,在这种情况下,为什么输出是正确的。还是输出错误

给定:

import scala.io.Source

val source = Source.fromString("abcdef")

val buff1 = source.buffered;

println("outer head 1: " +buff1.head)
println("outer head 2: " +buff1.head)

def readLine():List[String] = {
  def buffered = source.buffered
  def readLine(tokens:List[String] , partialToken:String):List[String] = {
    println("head1 " + buffered.head)
    println("head2 " + buffered.head)
    return Nil;
  }
  return (readLine(Nil, ""));
}

readLine();
对我来说,这个的预期输出是

outer head 1: a
outer head 2: a
head1: a
head2: a
实际产出如下

outer head 1: a
outer head 2: a
head1 b
head2 c

您是对的,只是它不是增加一个函数,而是增加一个简单的字段:在第66行,您可以使用一些IDE debbuger自己检查它,然后一步一步地执行。因此,您必须确保不要同时在多个位置使用它:
迭代器。在您的示例中,next
从3个不同的
BufferedSource
调用了3次,因此从中可以得到不同的值:

  • buff1.head
    :缓冲的源尚未缓冲任何内容,因此在此处要求
    head
    调用内部源的
    next
    ,因此第一个
    a
  • buff1.head
    再次:这里的head已经被缓冲,所以您得到了
    a
    ,并且内部源代码没有改变
  • buffered.head
    :由于
    buffered
    是一个
    def
    ,这相当于
    source.buffered.head
    。这个新的缓冲源尚未缓冲任何内容,因此请求
    head
    将从内部源检索一个元素,从而得到
    b
  • buffered.head
    :这将创建另一个缓冲源,如上所述,您将获得
    c
  • 底线是:如果调用
    source.buffered
    ,请不要再直接使用
    source
    ,也不要多次调用它

    您的示例可以通过立即调用
    buffered
    来修复:

    val source = Source.fromString("abcdef").buffered
    
    您还可以将
    def buffered=
    转换为
    val buffered=
    ,以确保
    source.buffered
    不会被多次调用

    在BufferEditor上调用head似乎是在内部函数中增加head

    注:(2016年7月3年后)

    显示:

    SI-9691
    buffereditor
    应公开
    headOption
    这将向
    buffereditor
    trait公开一个新的API。
    它将作为
    选项返回迭代器的下一个元素

    如果有下一个值,则返回
    Some(value)
    ,如果没有下一个元素,则返回
    None


    这将有助于避免任何形式的增量。

    正因为如此,我才能够完成此任务,谢谢。