我正在实施一个简单的内存中,类似redis的keyValue商店,并在if语句中遇到汇编失败以后的代码,以下代码:
/*
Returns the specified elements of the list stored at key. The offsets start and
stop are zero-based indexes, with 0 being the first element of the list to n. */
def lrange(k: keyT, maxIdx:Int, minIdx:Int): List[valT] = {
val l = lookup(k)
//assert(maxIdx >= minIdx && maxIdx <= (l length) && minIdx >= 0, "invalid min or max argument. list size ")
for {
(x: valT, i: Int) <- l zipWithIndex //tried without explicit typing
if i <= maxIdx && i >= minIdx //tried indenting if
} yield x
}
编辑器(Intellij)没有显示错误,但是在尝试构建和运行测试时,我会收到以下构建错误。
[INFO] --- scala-maven-plugin:3.3.2:compile (default) @ DS4300Project3 ---
[INFO] .../Spring2019/DS4300/scala/DS4300Project3/src/main/scala:-1: info: compiling
[INFO] Compiling 3 source files to .../Spring2019/DS4300/scala/DS4300Project3/target/classes at 1550678144065
[ERROR] .../Spring2019/DS4300/scala/DS4300Project3/src/main/scala/com/rejevichb/homework3/KeyValStore.scala:70: error: illegal start of simple expression
[ERROR] if (i <= maxIdx) && (i >= minIdx) //tried indenting if
[ERROR] ^
[ERROR] one error found
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
特别:
KeyValStore.scala:70: error: illegal start of simple expression
对这里出问题的任何指导或见解都对我来说是不清楚的。
这正是您应该谨慎使用后缀操作员的原因。
for {
i <- "a" zipWithIndex
if true
} yield i
被解析为
for { i <- ("a" zipWithIndex if true) } yield i
由于编译器试图将zipWithIndex
解释为二进制ifix操作员,但随后陷入if true
,这确实不是一个简单的表达式。
解决方案:
只是不使用后写操作,使用一个段:
for { i <- "a".zipWithIndex if true } yield i
添加一个半隆来强制
zipWithIndex
解释为后缀OP:for { i <- "a" zipWithIndex; if true } yield i
然后享受您的功能警告:
警告:应启用后缀操作员zipwithindex 通过使隐式值scala.language.postfixops可见。