编辑:根据原始答案重写这个问题
该scala.collection.immutable.Set班是不是在它的类型参数不变性.为什么是这样?
import scala.collection.immutable._
def foo(s: Set[CharSequence]): Unit = {
println(s)
}
def bar(): Unit = {
val s: Set[String] = Set("Hello", "World");
foo(s); //DOES NOT COMPILE, regardless of whether type is declared
//explicitly in the val s declaration
}
Run Code Online (Sandbox Code Playgroud) 我在Scala中理解Array组合时遇到了一些麻烦.
以下工作正常:
scala> Array('0', '1', '2') ++ Array('A', 'B', 'C')
res0: Array[Char] = Array(0, 1, 2, A, B, C)
Run Code Online (Sandbox Code Playgroud)
但是这个没有:
scala> ('0' to '9').toArray ++ ('A' to 'Z').toArray
<console>:8: error: polymorphic expression cannot be instantiated to expected type;
found : [B >: Char]Array[B]
required: scala.collection.GenTraversableOnce[?]
('0' to '9').toArray ++ ('A' to 'Z').toArray
^
Run Code Online (Sandbox Code Playgroud)
而且,以下似乎有效:
scala> ('0' to '9').toArray
res1: Array[Char] = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> ('A' to 'Z').toArray
res2: Array[Char] = Array(A, B, …Run Code Online (Sandbox Code Playgroud) 有人可以解释为什么以下不起作用.当我这样做时toSet,不知何故失去了编译类型推断的一些信息,但我不明白为什么.
scala> case class Foo(id: Int, name: String)
defined class Foo
scala> val ids = List(1,2,3)
ids: List[Int] = List(1, 2, 3)
scala> ids.toSet.map(Foo(_, "bar"))
<console>:11: error: missing parameter type for expanded function ((x$1) => Foo(x$1, "bar"))
ids.toSet.map(Foo(_, "bar"))
^
scala> ids.map(Foo(_, "bar")).toSet
res1: scala.collection.immutable.Set[Foo] = Set(Foo(1,bar), Foo(2,bar), Foo(3,bar))
Run Code Online (Sandbox Code Playgroud) 在Scala中,为什么在使用以下toSet功能时会发生以下情况TraversableOnce?
如果使用以下代码创建工作表(在IntelliJ中),您将获得以下输出(注意:使用Scala 2.10.2):
val maps = List(List(1,2),List(3,4),List(5,6,7),List(8),List())
maps.flatMap( _.map( _ + " " ) )
maps.flatMap( _.map( _ + " " ) ).toSet
maps.flatMap( _.map( _ + " " ) ).toSet()
Run Code Online (Sandbox Code Playgroud)
即res4产生一个布尔值
> maps: List[List[Int]] = List(List(1, 2), List(3, 4), List(5, 6, 7), List(8), List())
> res2: List[String] = List("1 ", "2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ")
> res3: scala.collection.immutable.Set[String] = Set("3 ", "8 ", "4 …Run Code Online (Sandbox Code Playgroud) 以下是我的错误:
trait Foo[A]
class Bar[A](set: Set[Foo[A]] = Set.empty)
Run Code Online (Sandbox Code Playgroud)
这产生了
<console>:8: error: polymorphic expression cannot be instantiated to expected type;
found : [A]scala.collection.immutable.Set[A]
required: Set[Foo[?]]
class Bar[A](set: Set[Foo[A]] = Set.empty)
^
Run Code Online (Sandbox Code Playgroud)
我必须重复类型参数,这非常烦人Set.empty.为什么类型推断失败了这个默认参数?以下作品:
class Bar[A](set: Set[Foo[A]] = { Set.empty: Set[Foo[A]] })
Run Code Online (Sandbox Code Playgroud)
请注意,这与此无关Set:
case class Hallo[A]()
class Bar[A](hallo: Hallo[A] = Hallo.apply) // nope
Run Code Online (Sandbox Code Playgroud)
奇怪的是,这不仅有效:
class Bar[A](hallo: Hallo[A] = Hallo.apply[A])
Run Code Online (Sandbox Code Playgroud)
......还有这个:
class Bar[A](hallo: Hallo[A] = Hallo()) // ???
Run Code Online (Sandbox Code Playgroud) 为什么这段代码不起作用:
scala> List('a', 'b', 'c').toSet.subsets.foreach(e => println(e))
<console>:8: error: missing parameter type
List('a', 'b', 'c').toSet.subsets.foreach(e => println(e))
^
Run Code Online (Sandbox Code Playgroud)
但是当我拆分它然后它工作正常:
scala> val itr=List('a', 'b', 'c').toSet.subsets
itr: Iterator[scala.collection.immutable.Set[Char]] = non-empty iterator
scala> itr.foreach(e => println(e))
Set()
Set(a)
Set(b)
Set(c)
Set(a, b)
Set(a, c)
Set(b, c)
Set(a, b, c)
Run Code Online (Sandbox Code Playgroud)
这段代码也可以:
Set('a', 'b', 'c').subsets.foreach(e => println(e))
Run Code Online (Sandbox Code Playgroud) 我有两个解决方案,但一个不编译,另一个,我认为,可能更好:
object Foo extends App {
val vectors = List(List(1,2,3), List(2,2,3), List(1,2,2)) //just a stupid example
//transposing
println("vectors = " + vectors.transpose.map (_.sum)) //it prints vectors = List(4, 6, 8)
//folding
vectors.reduce {
case (a, b) => (a zip b) map {
case (x, y) => x + y
}
} //compiler says: missing parameter type for exp. function; arg. types must be fully known
}
Run Code Online (Sandbox Code Playgroud)