不能证明 Unit <:<(T, U) [英] Cannot prove that Unit <:< (T, U)

查看:44
本文介绍了不能证明 Unit <:<(T, U)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试从列表中删除所有 Unit - () 时,我尝试调用 toMap.

When trying to remove all Unit - () from a list, I tried to call toMap.

scala> List((), ()).filter(_ != ()).toMap
<console>:8: error: Cannot prove that Unit <:< (T, U).
              List((), ()).filter(_ != ()).toMap
                                           ^

这个错误是什么意思?

对于 List,我想为非 Unit 元素创建所有元组 (String, String) 的映射,但某些值可以是空.

For a List, I'd like to create a map of all tuples (String, String) for non-Unit elements, but some of the values can be null.

scala> val x = List((), (), (3,4)).filter(_ != ()).toMap
<console>:7: error: Cannot prove that Any <:< (T, U).
       val x = List((), (), (3,4)).filter(_ != ()).toMap
                                                   ^

scala> val x = List((), (), (3,4)).filter(_ != ())
x: List[Any] = List((3,4))

scala> x.toMap
<console>:9: error: Cannot prove that Any <:< (T, U).
              x.toMap
                ^

推荐答案

啊!现在您的其他问题更有意义.仍然不确定你在做什么来生成这个混合的 Unit/Tuple2 列表.

Ah! Now your other question makes a little more sense. Still not sure what you're doing to produce this mixed Unit/Tuple2 list though.

这应该有效:

List((), (), (3,4)).collect { case t@(_: Int, _: Int) => t }.toMap

请注意,我使用的是 变量绑定在这里(将匹配绑定到t)返回我们匹配的同一个Tuple2实例,而不是创建一个新的实例.

Note that I'm using variable binding here (binding the match to t) to return the same Tuple2 instance we matched rather than creating a new one.

通过使用 collect,您可以将列表类型从 List[Any] 转换为 List[(Int, Int)],即是 toMap 想要的,因为它期待一些 List[(A,B)].

By using collect you convert the type of your list from List[Any] to List[(Int, Int)], which is what toMap wants since it's expecting some List[(A,B)].

注意:虽然这个答案应该适合你,但我仍然认为你的设计有缺陷.您最好修复潜在的设计缺陷,而不是像这样处理症状.

看起来这很适合使用 Scala 的Option 类型.在这种情况下,您的示例列表将变为 List(None, None, Some((3,4))),或者您可以将其写为 List(None, None, Some(3)->4)) 以提高可读性(像这样的嵌套括号可能会引起混淆).

It looks like this would be a good fit for using Scala's Option type. In this case, your sample list would become List(None, None, Some((3,4))), or you could write it as List(None, None, Some(3->4)) for readability (nested parenthesis like that can get confusing).

如果你使用 Option 那么你的列表的类型变成了 List[Option[(Int, Int)]],这应该比一个更好的处理List[Any].要摆脱 None 条目并获得所需的 List[(Int,Int)],您只需调用 flatten:

If you use Option then the type of your list becomes List[Option[(Int, Int)]], which should be much nicer to deal with than a List[Any]. To get rid of the None entries and get the desired List[(Int,Int)] you can just call flatten:

List(None, None, Some(3->4)).flatten
// res0: List[(Int, Int)] = List((3,4))
List(None, None, Some(3->4)).flatten.toMap
// res1: scala.collection.immutable.Map[Int,Int] = Map(3 -> 4)

但是,如果您可以避免将 None 条目放在列表中,那会更好.如果您使用 Scala 生成此列表以进行理解,您可以使用for 表达式中的保护 以从输出中删除无效元素.

However, it would be even better if you can avoid putting the None entries in your list in the first place. If you're producing this list using a Scala for comprehension, you could use a guard in your for expression to remove the invalid elements from the output.

这篇关于不能证明 Unit &lt;:&lt;(T, U)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆