相关疑难解决方法(0)

为什么Scala的不可变Set在其类型中不协变?

编辑:根据原始答案重写这个问题

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 set covariance scala-collections

89
推荐指数
3
解决办法
7386
查看次数

在Scala中组合数组时的有趣发现

我在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)

arrays scala

12
推荐指数
1
解决办法
590
查看次数

设置和输入推断

有人可以解释为什么以下不起作用.当我这样做时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

10
推荐指数
1
解决办法
533
查看次数

Scala TraversableOnce和toSet

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)

scala

9
推荐指数
1
解决办法
2050
查看次数

错误:带有默认参数的多态表达式

以下是我的错误:

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 type-inference default-value

8
推荐指数
1
解决办法
2080
查看次数

调用toSet丢失参数类型错误

为什么这段代码不起作用:

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)

scala

6
推荐指数
1
解决办法
1356
查看次数

在Scala列表中添加所有向量(在数学意义上)的最快惯用方法是什么?

我有两个解决方案,但一个不编译,另一个,我认为,可能更好:

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)

scala list

0
推荐指数
1
解决办法
115
查看次数