Why is scala.collection.immutable.List[Object] not GenTraversableOnce[?]

↘锁芯ラ 提交于 2019-12-13 04:16:19

问题


Simple question, and sorry if this is a stupid question as I am just beginning in scala. I am getting a type mismatch error that says:

found   : (AnyRef, org.apache.tinkerpop.gremlin.hadoop.structure.io.VertexWritable) => List[Object]
required: ((AnyRef, org.apache.tinkerpop.gremlin.hadoop.structure.io.VertexWritable)) => scala.collection.GenTraversableOnce[?]

But according to this post (I have a Scala List, how can I get a TraversableOnce?), a scala.collection.immutable.List is an Iterable and therefore also a GenTraversableOnce. And yet this error seems to indicate otherwise. And furthermore, when I actually look at the link in the accepted answer of that post, I don't see any reference to the word "traversable".

If the problem has to do with my inner class not being correct, then I have to say this error is extremely uninformative, since requiring that the inner class be of type "?" is obviously a vacuous statement ... Any help in understanding this would be appreciated.


回答1:


Function2[X, Y, Z] is not the same thing as Function1[(X, Y), Z].

Compare these two definitions:

val f: ((Int, Int)) => Int = xy => xy._1 + xy._2
val f: (Int, Int) => Int = (x, y) => x + y

The first could also be written with a pattern-matching, that first decomposes the tuple:

val f: ((Int, Int)) => Int = { case (x, y) => x + y }

This is exactly what the error message asks you to do: provide an unary function that takes a tuple as argument, not a binary function. Note that there is the tupled-method, that does exactly that.

The return types of the functions are mostly irrelevant here, the compiler doesn't get to unify them, because it fails on the types of the inputs.


Also related:

  1. Same story with eta-expansions: Why does my implementation of Haskell snd not compile in Scala


来源:https://stackoverflow.com/questions/52253836/why-is-scala-collection-immutable-listobject-not-gentraversableonce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!