scala "Illegal start of simple expression" 用于理解 if
scala "Illegal start of simple expression" in for comprehension with if
我正在实现一个简单的内存中、类似 Redis 的 KeyValue 存储,并且在 for comprehension 以下代码段中的 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
解释为二进制中缀运算符,但随后遇到if true
,这确实不是一个简单的表达式。
解决方法:
只是不要使用后缀操作,使用句点:
for {
i <- "a".zipWithIndex
if true
} yield i
添加一个分号强制zipWithIndex
被解释为后缀op:
for {
i <- "a" zipWithIndex;
if true
} yield i
然后享受你的功能警告:
warning: postfix operator zipWithIndex should be enabled
by making the implicit value scala.language.postfixOps visible.
我正在实现一个简单的内存中、类似 Redis 的 KeyValue 存储,并且在 for comprehension 以下代码段中的 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
解释为二进制中缀运算符,但随后遇到if true
,这确实不是一个简单的表达式。
解决方法:
只是不要使用后缀操作,使用句点:
for { i <- "a".zipWithIndex if true } yield i
添加一个分号强制
zipWithIndex
被解释为后缀op:for { i <- "a" zipWithIndex; if true } yield i
然后享受你的功能警告:
warning: postfix operator zipWithIndex should be enabled by making the implicit value scala.language.postfixOps visible.