我正在尝试在脚本中编写2个函数,并得到我无法解决的类型不匹配。这是示例代码:
def generate(start: Int, end: Int): Seq[Int] = (start until end).toSeq
def restrain(seq: Seq[Int]) = seq.dropWhile(_ < 20).takeWhile(_ < 60)
val com: (Int, Int) => Seq[Int] = (restrain _ compose generate)
通过在REPL中加载以下内容:
:load test.sc
我收到以下错误:
val com: (Int, Int) => Seq[Int] = (restrain _ compose generate)
^
test.sc:1: error: type mismatch;
found : (Int, Int) => Seq[Int]
required: ? => Seq[Int]
我究竟做错了什么?
类型Function2[Int, Int, Seq[Int]]
和Function1[(Int, Int), Seq[Int]]
不相同。在(generate _)
生产前,而这种成分,你需要后者。尝试:
restrain _ compose (generate _).tupled
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句